手机游戏3D人物的移动控制

[RequireComponent(typeof(CharacterController))]
public class MoveTest : MonoBehaviour {
    

    private Rigidbody myRigidbody;
    private CharacterController myCharacterController;
    private Vector3 moveDirection = Vector3.zero;


    public float my_重力 = 20f;
    public float my_速度 = 5f;
    public float my_跳跃速度 = 10f;
    
    //人物的各种状态,包括可超控,不可操作,不能移动,不能攻击,死亡等
    public UnitStateEnum my_状态 = UnitStateEnum.free;
    // Use this for initialization
    void Start () {

        //定义CharacterController组件
        myCharacterController = transform.GetComponent<CharacterController>();
        //设定人物不可穿透
        myCharacterController.isTrigger = false;

    }
	
	
	void Update () {
	
	}
    void FixedUpdate()
    {
        //当人物在地面上时候,并且可控制状态
        if (myCharacterController.isGrounded&&( my_状态== UnitStateEnum.free|| my_状态 == UnitStateEnum.slow))
        {
            //移动方向等于Input的横轴和纵轴位移,Y(上下方向不变)
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
           
            //让人物面朝着你需要移动的方向
            transform.LookAt(transform.position+ moveDirection);
           //定义速度
            moveDirection *= my_速度;
            //跳跃,如果获取到了跳跃键,则Y轴跳跃
            if (Input.GetButton("Jump"))
                moveDirection.y = my_跳跃速度;
        }
        //附加重力因素,Y的方向一直在向下受重力影响
        moveDirection.y -= my_重力 * Time.deltaTime;
        //向控制的方向移动
        myCharacterController.Move(moveDirection * Time.deltaTime);

    
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值