UNITY之CharacterController

//Demo01

using UnityEngine;

using System.Collections;
public class CharacterCtr : MonoBehaviour {
    public Vector3 targetPos;
    CharacterController cc;

    // Use this for initialization
    void Start () {
        cc = gameObject.GetComponent<CharacterController> ();
    }
    
    // Update is called once per frame
    void Update () {
    
        if(Input.GetMouseButton(0)){
            //从摄像机位置到nput.mousePosition(鼠标点)的位置构造一条射线ray
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            //绘制射线
            Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
            //RaycastHit射线投射碰撞信息
            //Variables>collider:碰到的碰撞器,distance:从射线的原点到触碰点的距离
            //point:在世界坐标空间,射线碰到碰撞器的接触点
            //rigidbody:碰到的该碰撞器上的刚体,如果碰撞器上没有附加刚体,那么返回null
            //transform:碰到的该刚体或碰撞器的变换
            RaycastHit hit;
            if(Physics.Raycast(ray,out hit)){
                targetPos = hit.point;
            }
        }

        if(Vector3.Distance(targetPos,transform.position)>1f){
            Vector3 step = Vector3.ClampMagnitude (targetPos-this.transform.position,0.1f);
            cc.Move(step);

        }

        if(Input.GetKey(KeyCode.Space)){
            //translate不会触发OnControllerColliderHit此函数,只有使用move是才会有碰撞检测
            this.transform.Translate (Vector3.up);
            //cc.Move (Vector3.up);
        }
    }
    //ControllerColliderHit:角色控制器碰撞器碰撞信息
    //Variables:
    //collider:由角色控制器碰到的碰撞器,被碰到GameObject的collider
    //controller:碰到的角色控制器,也就角色控制器组件
    //gameObject:角色控制器碰到的该碰撞器的游戏对象
    //moveDirection:当发生碰撞,角色控制移动的方向
    //point:在世界坐标空间的碰撞点
    //rigidbody:有角色控制器碰到的刚体
    //transform:角色控制器碰到的碰撞器的Trangsform
    void OnControllerColliderHit(ControllerColliderHit hit){//碰撞检测函数
        if (hit.collider.gameObject.name == "Cube") {
            print ("hit");
        }
        //只要碰到Collider则为true,在空中没碰到任何collider则为false
        print (cc.isGrounded);

        print (hit.gameObject.name);//Cube

    }

}

//Demo02

using UnityEngine;
using System.Collections;

public class CharacterCtr : MonoBehaviour {
    public CharacterController cc;
    private Animation anim;
    public EasyJoystick joy;
    private Vector3 moveDirection=Vector3.zero;
    public float moveSpeed = 5.0f;
    public float pushPower = 2.0f;
    // Use this for initialization
    void Start () {
    
        cc = this.GetComponent<CharacterController> ();
        anim = this.GetComponent<Animation> ();
    }
    
    // Update is called once per frame
    void Update () {
    
        float x = joy.JoystickTouch.x;
        float z = joy.JoystickTouch.y;
        if (x != 0 || z != 0) {
            transform.LookAt (new Vector3 (transform.position.+ x, transform.position.y, transform.position.+ z));
            //TransformDirection将局部坐标系转变成世界坐标系
            moveDirection = transform.TransformDirection (Vector3.forward);
            moveDirection = moveDirection * 4.0f * Time.deltaTime;

            moveDirection.-= 9.8f * Time.deltaTime;
            //move只接受世界坐标系,move里有内置检测,当碰到刚体时,则不会随moveDirection.y向下移动

            //move(相对世界坐标系的运动方向)
            CollisionFlags flags = cc.Move (moveDirection);

            anim.CrossFade ("Run", 0.5f);//播放动画
            print (cc.isGrounded);
        } else {
        
            anim.CrossFade ("Idle",0.5f);
        }
        if(Input.GetKeyDown(KeyCode.Space)){
            this.transform.Translate (Vector3.up);
        }
    }

    void OnControllerColliderHit(ControllerColliderHit hit){
        print ("hit");
        //Collider:碰撞器
        //Varianle>attachedRigidbody:碰撞器附加的刚体组件
        Rigidbody body = hit.collider.attachedRigidbody;
        if(body ==null || body.isKinematic){
            return;
        }
        Vector3 pushDir = new Vector3 (hit.moveDirection.x,0,hit.moveDirection.z);
        body.velocity = pushDir * pushPower;

        print (hit.collider.gameObject.name);

        print (hit.controller.gameObject.name);

        print (hit.gameObject.name);
    }
}


//这个脚本基于方向键向前
//和两边移动角色控制器
//按下空格后它也会跳跃
//确保这个与角色控制器附加在同一个游戏物体上
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
function FixedUpdate() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
//
我们在地面上,因此重计算
//直接沿轴方向移动
moveDirection = 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);
}

using UnityEngine;
using System.Collections;

public class TestClampMagnitude : MonoBehaviour {
    private CharacterController cc;
    private Vector3 MoveDir=Vector3.zero;
    private Vector3 targetPos;
    private bool startMove=false;
    // Use this for initialization
    void Start () {
        cc = this.GetComponent<CharacterController> ();
        targetPos=this.transform.position;
    }
    
    // Update is called once per frame
    void Update () {
        PlayerMove ();
    }
    void PlayerMove(){
        if(Input.GetMouseButton(0)){
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            if(Physics.Raycast(ray,out hit)){
                targetPos = hit.point;
                this.transform.LookAt (targetPos);
            }
            startMove = true;
        }
        if (Vector3.Distance (this.transform.position, targetPos) > 0.5f && startMove) {
            print ("gogo");
            MoveDir = this.transform.TransformDirection (Vector3.forward) * 2f;
            MoveDir.y -= 9.8f;
            if(Input.GetKey(KeyCode.Space)){
                MoveDir.y += 40f;
            }
            cc.Move (MoveDir * Time.deltaTime);
        } else {
            startMove = false;
        }
        if(Input.GetKey(KeyCode.Space)){
            MoveDir = this.transform.TransformDirection (Vector3.zero);
            MoveDir.y += 40f;
            print (MoveDir);
            cc.Move (MoveDir * Time.deltaTime);
        }
    }
}

你可能感兴趣的:(UNITY之CharacterController)