플레이어 배치 및 전진
Unity-ShootingProto/Player 2017. 8. 12. 19:55 |1. 플레이어가 이동할 바닥 만들기
1-1. Create -> 3D Object -> Cube 를 생성
1-2. Cube Transform -> Position X:0, Y:-0.6, Z:0 Scale X:13, Y:1, Z:23
1-3. Cube Material -> MatGrass
1-4. Cube Edit -> Duplicate 10번 실행 Z position을 각각 0, 23, 46, 69, 92, 115, 138, 161, 184, 207 로 지정 (23씩 증가)
2. MatGrass 만들기
2-1. Create -> Material
2-2. Shader -> Unlit/Texture
2-3. Texture -> rough_grass.jpg를 등록해서 선택
2-4. Tiling X:18, Y:32
3. 플레이어 배치
3-1. Create -> Create Empty (이름을 Player로 지정)
3-2. Kenney ->Model -> advancedCharacter 프리팹을 Player로 Drag&Drop (이름을 Model로 지정)
3-3. Model Scale X:20, Y:20, Z:20
3-4. Model 하위에 있는 ArmLeft1, ArmRight1, Body1, Head1, LegLeft1, LegRight1 에 skin_woman material 지정
3-5. Player에 Rigidbody 추가 - Use Gravity: false
3-6. Player에 Capsule Collider 추가 - Center X:0, Y:1.7, Z:0 Radius:0.8 Height:3.6 Direction:Y-Axis
4. 플레이어를 따라 다니는 카메라 만들기
4.1 게임 플레이 뷰 - 1080 * 1920 으로 생성
4.2 Main Camera - (_MainCamera 로 이름 변경)
4.3 _MainCamera 설정
- Position X:0, Y:10, Z:4.8
- Rotation X:76.7, Y:0, Z:0
- Scale X:1, Y:1, Z:1
- Clear Flags : Solid Color
- Background : Black
- Culling Mask : Everything
- Projection : Orthographic
- Size : 9.6
- Clipping Planes Near:0.3, Far:100
4.4 Create -> C# Script (이름을 FollowCamCS 로 지정)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCamCS : MonoBehaviour {
public Transform playerTransform;
private Vector3 camOffset = new Vector3(0f, 10f, 4.8f);
private void FixedUpdate()
{
// 플레이어가 앞으로 움직일 때만 카메라가 플레이어를 따라간다.
// 좌우 이동일 때는 카메라는 움직이지 않음
if (PlayerCS.S.moving)
{
Vector3 pos = transform.position;
Vector3 pos1 = playerTransform.position + camOffset;
pos1.x = pos.x;
transform.position = Vector3.Lerp(pos, pos1, 0.5f);
}
}
}
5. 게임 컨트롤러 만들기
5.1 Create -> Create Empty (이름을 GameController로 지정)
5.2 Create -> C# Script (이름을 GameControllerCS로 지정)
5.3 GameController 게임오브젝트에 GameControllerCS 컴포넌트를 추가
5.4 GameControllerCS 편집
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameControllerCS : MonoBehaviour {
public static GameControllerCS S;
private void Awake()
{
S = this;
}
private void Start()
{
InitGame();
}
private void InitGame()
{
}
}
6. 플레이어 전진
6.1 목표 : 플레이어는 항상 전진한다.
6.2 Create -> C# Script (이름을 PlayerCS로 지정)
6.3 PlayerCS 편집
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCS : MonoBehaviour {
public static PlayerCS S;
public bool playing = false;
public bool moving = false;
[System.NonSerialized]
public Vector3 moveSpeed = new Vector3(0f, 0f, 5f);
private Rigidbody playerRB;
private void Awake()
{
S = this;
playerRB = GetComponent<Rigidbody>();
}
public void StartMoving ()
{
moving = true;
playerRB.velocity = moveSpeed;
}
public void StopMoving ()
{
moving = false;
playerRB.velocity = Vector3.zero;
}
}
6.4 GameControllerCS - InitGame() 함수 편집
private void InitGame()
{
PlayerCS.S.StartMoving();
PlayerCS.S.playing = true;
}
6.5 Player -> Rigidbody -> Constraints -> Freeze Rotation X:check, Y:not check, Z:check (플레이어 좌우로만 회전 가능)
'Unity-ShootingProto > Player' 카테고리의 다른 글
플레이어 이동 (0) | 2017.08.12 |
---|---|
플레이어 생성 (0) | 2017.08.12 |