using UnityEngine;
using System.Collections; public class man_control : MonoBehaviour { // Use this for initialization
public float speed=1.0f;
public float jumpspeed=1.0F;
public float gravity=5.0F;
float A_Rotate_Speed=50.0f;
private Vector3 moveDirection=Vector3.zero;
void Start () {
}
// Update is called once per frame
void Update () {
CharacterController controller=GetComponent<CharacterController>();
if(controller.isGrounded)
{
moveDirection=new Vector3(0,0,Input.GetAxis("Horizontal"));
moveDirection=transform.TransformDirection(moveDirection);
moveDirection*=speed;
if(Input.GetKey(KeyCode.Space))
{
moveDirection.y=jumpSpeed;
}
}
moveDirection.y-=gravity*Time.deltaTime;
controller.Move(moveDirection*Time.deltaTime);
if(Input.GetKeyDown(KeyCode.Q))
{
transform.Rotate(new Vector3(0,90,0),Space.Self);
}
if(Input.GetKeyDown(KeyCode.E))
{
transform.Rotate(new Vector3(0,-90,0),Space.Self);
}
}
} //除了这个之外,charactercontroller默认自带有刚体,使用Move函数不会启用重力,需要同上自己写。使用SimpleMove函数会自动启用重力。
//另外,一个charactercontroller需要和一个自带有rigidbody组件的物体才能产生碰撞。
|