Unity/유니티 개발
[유니티 C#] 캐릭터 이동 회전 스크립트
도이(doi)
2021. 10. 18. 11:47
728x90
간단하게 캐릭터 테스트할 때 요긴하게 쓰이는
캐릭터 이동 회전 스크립트.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour
{
public float speed = 10.0f;
public float rotationSpeed = 0.5f;
private void Update()
{
float h = Input.GetAxis("Horizontal")*speed*Time.deltaTime;
float v = Input.GetAxis("Vertical")*speed*Time.deltaTime;
Vector3 direction = new Vector3(h, 0, v);
//이동했을 때
if(direction != Vector3.zero)
{
float angle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
//뒤를 바라볼 때는 이동만
if(Mathf.Abs(angle) < 180)
{
angle = angle * rotationSpeed * Time.deltaTime;
transform.Rotate(Vector3.up, angle);
}
}
transform.position += direction * speed * Time.deltaTime;
}
}
728x90
반응형