-
[3D] 오브젝트 이동구현 및 애니메이션 적용 -3Unity(유니티) 2022. 4. 20. 11:10
* Transform.GetChild
public Transform Getchild(int index);
index : 해당 트랜스폼이 부모 트랜스폼을 갖지 않은 경우에 Transform.position과 같음
[MainScript]
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test3 : MonoBehaviour { public float moveSpeed; private float hAxis; private float vAxis; private Vector3 moveVec; private bool isMove; [SerializeField] private GameObject chRig; private Animator anim; private GameObject chaBody; void Start() { chaBody = transform.GetChild(0).gameObject; chRig = transform.GetChild(0).gameObject; isMove = false; anim = chaBody.GetComponent<Animator>(); } void Update() { hAxis = Input.GetAxisRaw("Horizontal"); //키 값을 받음 vAxis = Input.GetAxisRaw("Vertical"); moveVec = new Vector3(hAxis, 0, vAxis); //입력받은 키에 대한 방향값 isMove = moveVec.magnitude > 0; //움직임 유무 판단 if (isMove) { transform.Translate(moveVec * Time.deltaTime * moveSpeed); //캐릭터 이동명령 chRig.transform.forward = moveVec; anim.SetBool("isMove", true); } else anim.SetBool("isMove", false); } }
'Unity(유니티)' 카테고리의 다른 글
[JohnLemon 3D] 첫 번째 스크립트 작성하기(void Start) (0) 2022.04.20 [3D] 코루틴 사용하기 (0) 2022.04.20 [3D] 오브젝트 이동구현-2 (0) 2022.04.20 [3D] 오브젝트 이동 구현-1 (0) 2022.04.20 [3D] 카메라 이동 및 회전(zoom in, out) (0) 2022.04.20