角色控制器(CharacterController)

移动:

  1、SimpleMove(Vector3: vector3&speed)

    简单移动,可以根据vector3方向移动,物体不需要添加刚体即受重力影响,不需要添加碰撞器即可以产生碰撞,但无法推动其它物体。

  2、Move(Vector3: vector3&speed)

    移动,根据vector3方向移动,速度比SimpleMove快许多,不受重力影响,但可以在不添加碰撞器的情况下产生碰撞,无法推动其它物体。

sample

using UnityEngine;
using System.Collections; public class CharControl : MonoBehaviour { CharacterController charCtl ;
// Use this for initialization
void Start () {
charCtl = GetComponent<CharacterController>() ;
} // Update is called once per frame
void Update () {
charCtl.Move(new Vector3(Input.GetAxis("Horizontal"), , Input.GetAxis("Vertical"))* 0.5f) ;
// charCtl.SimpleMove(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))* 10) ;
}
}

碰撞:

  固定代码,需要添加刚体,也可以不添加刚体,改变函数中判断

    public float pushPower = 2.0F;
void OnControllerColliderHit(ControllerColliderHit hit) {
Rigidbody body = hit.collider.attachedRigidbody;//没有刚体返回空
if (body == null || body.isKinematic)
return; if (hit.moveDirection.y < -0.3F)//被碰撞物体在它下面
return; Vector3 pushDir = new Vector3(hit.moveDirection.x, , hit.moveDirection.z);
body.velocity = pushDir * pushPower;
}

使一人物围绕三个点自动巡逻  转弯时为平滑转弯

两种方法:

  1、 改变transform.forward

  2、 改变transform.rotation

using UnityEngine;
using System.Collections; public class CharControl : MonoBehaviour { CharacterController charCtl ;
public Transform[] point ;
public Transform nextPoint ;
Transform t1 ;
public int index ;
// Use this for initialization
void Start () {
charCtl = GetComponent<CharacterController>() ;
index = ;
nextPoint = point[] ;
} // Update is called once per frame void Update () { // charCtl.Move(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))* 0.5f) ;
// charCtl.SimpleMove(new Vector3(0, 0, Input.GetAxis("Vertical"))* 10) ; // changeForward() ;
changeRotation() ;
}
void changeForward(){
if(Vector3.Distance(IgnoreY(transform.position), IgnoreY(nextPoint.position))>0.2f){
Vector3 direction = (IgnoreY(nextPoint.position)-IgnoreY(transform.position)).normalized ;//must be normalized
transform.forward = Vector3.Lerp(transform.forward, direction, 0.1f);
// charCtl.transform.forward = direction;
// charCtl.transform.LookAt(nextPoint.position);
charCtl.SimpleMove(transform.forward*) ;
}else{
index = (index+)%point.Length ;
nextPoint = point[index] ;
}
}
void changeRotation(){
if(Vector3.Distance(IgnoreY(transform.position), IgnoreY(nextPoint.position))>0.2f){
Vector3 direction = (IgnoreY(nextPoint.position)-IgnoreY(transform.position)).normalized ;
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, 0.1f);
charCtl.SimpleMove(transform.forward*) ; }else{
index = (index+)%point.Length ;
nextPoint = point[index] ; }
} Vector3 IgnoreY(Vector3 x){
return new Vector3(x.x, , x.z) ;
}
public float pushPower = 2.0F;
void OnControllerColliderHit(ControllerColliderHit hit) {
Rigidbody body = hit.collider.attachedRigidbody;//no rigidbody return null
if (body == null || body.isKinematic)
return; if (hit.moveDirection.y < -0.3F)
return; Vector3 pushDir = new Vector3(hit.moveDirection.x, , hit.moveDirection.z);
body.velocity = pushDir * pushPower;
} }
上一篇:windows win10上传文件到linux服务器


下一篇:cygwin install git