Unity - Character Controller

반응형

케릭터를 스피드값을 이용해서 움직이기.

 using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
public class example : MonoBehaviour
{
public float speed = 3.0F;
public float rotateSpeed = 3.0F;
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
float curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
}
}
 
좀 더 복잡한 이동을 지원.public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;

}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}

 

'Unity > Unity Study' 카테고리의 다른 글

void OnDrawGizmos()  (0) 2013.12.18
unity - camera smooth follow  (0) 2013.11.16
unity - time 출력  (0) 2013.10.30
unity - time 관련 함수  (0) 2013.10.30
unity 2d sprite animation  (0) 2013.10.29
TAGS.

Comments