【发布时间】:2015-02-07 18:16:12
【问题描述】:
代码很好地使用左右箭头移动刚体,但我不明白使用 maxVelocity 和 absVelX 有什么意义,我不知道他们在代码中的工作..当我删除两者都很好用,但似乎我在高级游戏级别中都需要。这是 lynda 的课程形式,名为“杰西·弗里曼的 Unity 2d 基本培训”
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float speed=10f;
public Vector2 maxVelocity=new Vector2(3,5);
public bool standing;
public float jetSpeed=15f;
// Update is called once per frame
void Update () {
//Force resetted each frame
var forceX = 0f;
var forceY = 0f;
var absVelX = Mathf.Abs (rigidbody2D.velocity.x);
var absVelY = Mathf.Abs (rigidbody2D.velocity.y);
if (Input.GetKey ("right")) {
if (absVelX < maxVelocity.x)
forceX = speed;
transform.localScale=new Vector3(1,1,1);//Sprite orignal pose
} else if (Input.GetKey ("left")) {
if (absVelX < maxVelocity.x)
forceX = -speed;
transform.localScale=new Vector3(-1,1,1);//Sprite reversal pose
}
rigidbody2D.AddForce(new Vector2(forceX,forceY));
}
}
【问题讨论】:
标签: c# unity3d game-engine unityscript