移动:

  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"), 0, Input.GetAxis("Vertical"))* 0.5f) ;
//        charCtl.SimpleMove(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))* 10) ;
    }
}
View Code

 

碰撞:

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

    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, 0, hit.moveDirection.z);
        body.velocity = pushDir * pushPower;
    }
View Code

 

 

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

两种方法:

  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 = 0 ;
        nextPoint = point[0] ;
    }
    
    // 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*10) ;
        }else{
            index = (index+1)%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*10) ;

        }else{
            index = (index+1)%point.Length ;
            nextPoint = point[index] ;
            
        }
    }
    
    
    Vector3 IgnoreY(Vector3 x){
        return new Vector3(x.x, 0, 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, 0, hit.moveDirection.z);
        body.velocity = pushDir * pushPower;
    }

}

 

posted on 2013-08-21 10:09  追逐.  阅读(2817)  评论(0编辑  收藏  举报