Rigidbody2D rig;
float movespeed;
float jumpspeed;
// Use this for initialization
void Start ()
{
rig = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
float t = Input.GetAxisRaw("Vertical");//这个不具有平滑的滤波的应用 只能有-1 0 1的变化
float v2 = Input.GetAxis("Vertical");//这个具有平滑的变化
if (Input.GetAxisRaw("Horizontal") > 0)
{
rig.velocity = new Vector2(movespeed, rig.velocity.y);
}
else if (Input.GetAxisRaw("Vertical") < 0)
{
rig.velocity = new Vector2(-movespeed, rig.velocity.y);
}
else
{
rig.velocity = new Vector2(0, rig.velocity.y);
}
if (Input.GetButtonDown("Jump"))//这个是按空格触发
{
rig.velocity = new Vector2(rig.velocity.x, jumpspeed);
}
如果在2D游戏中可以使用上面的代码进行控制人物的左右移动,跳跃, 如果不适用GetAxisRaw 人物会有平滑的效果。