作用:控制角色CharacterController
1、SimpleMove(Vector3: vector3&speed)重力
简单移动,可以根据vector3方向移动,物体不需要添加刚体即受重力影响,不需要添加碰撞器即可以产生碰撞,但无法推动其它物体。
2、Move(Vector3: vector3&speed)无重力
移动,根据vector3方向移动,速度比SimpleMove快许多,不受重力影响,但可以在不添加碰撞器的情况下产生碰撞,无法推动其它物体。
using UnityEngine;
using System.Collections;
public class PlayerCC : MonoBehaviour {
public float speed = 3;
private CharacterController charCtl ;
// Use this for initialization
void Start () {
charCtl = GetComponent<CharacterController>() ;
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
charCtl.SimpleMove(new Vector3(h, 0, v)* speed) ;
charCtl.Move(new Vector3(h, 0, v)* speed* TimeDeltaTime) ;
}
}
using UnityEngine;
using System.Collections;
public class PlayerCC : MonoBehaviour {
public float speed = 3;//速度
private CharacterController charCtl ;//目标物体
// Use this for initialization
void Start () {
charCtl = GetComponent<CharacterController>() ;//目标物体,获得组件
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");//变量
float v = Input.GetAxis("Vertical");//变量
charCtl.SimpleMove(new Vector3(h, 0, v)* speed) ;//方法1
charCtl.Move(new Vector3(h, 0, v)* speed* TimeDeltaTime) ;//方法2
}
}
1.物体添加CharacterController组件
2.注意文件命名空间
3.多用SimpleMove方法