rigidbody.velocity 控制角色的移动

rigidbody.velocity 控制角色的移动


using UnityEngine;
using System.Collections;

public class SphereMove : MonoBehaviour {

    public float MoveSpeed = 4.0f;

	void Start () {
        
	}
	
	
	void Update () {

        //获得刚体的向量速度
        Vector3 vel = rigidbody.velocity;


        //Edit -> Project Setting -> Input 可以添加用户自定义的按键
        

        //获取水平方线
        float h = Input.GetAxis("Horizontal");//默认 Horizontal a键 为 -1  d键为 1

        float v = Input.GetAxis("Vertical");//默认 Horizontal s键 为 -1  w键为 1


        if (Mathf.Abs(h) > 0.05f || Mathf.Abs(v) > 0.05f)
        {
            //给刚体更新新的向量速度
            rigidbody.velocity = new Vector3(-h * MoveSpeed, vel.y, -v * MoveSpeed);
            transform.rotation = Quaternion.LookRotation(new Vector3(-h, 0, -v));
        }

        //Input中默认Jump 键为space(空格)键
        if (Input.GetButtonDown("Jump"))
        {
            rigidbody.velocity = new Vector3(0f, 10f, 0f);
        }



	}
}


你可能感兴趣的:(Unity基础)