Unity/Unity Study

Unity - Character Controller

붕대마음 2013. 11. 3. 16:12
반응형

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

 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);
}