【问题标题】:C# Player move in UnityC# Player 在 Unity 中移动
【发布时间】: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


    【解决方案1】:

    从提供的代码看来,这些变量是定义的速度限制,分配给rigidbody2D.velocity.x(对abs 的调用是比较绝对向量幅度而不仅仅是坐标)。

    不清楚是否设置了rigidbody2D.velocity.x,但代码假定它可能比3 更大,并且程序会在该最大值处中断。

    【讨论】:

    • 那么代码是在每帧之后将rigidbody2D.velocity.x 分配给absvelX 吗?所以在任何速度应用之前它的标量现在设置为“0”?
    • Abs 表示移除标志。因此,如果您的 rigidbody2D.velocity.x 等于 -0.34,则它变为 0.34。
    • 我知道,我的意思是,absvelX 总是 == 0 吗? ,和移动玩家之前一样,没有速度值!
    • 如果 velocity.x 的值为 0,abs 也会,是的。
    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多