1

我正在为我的学习制作游戏,但我已经看过我的讲师并搜索了几个小时,但没有找到解决方案。游戏是这样工作的:

用户按住 Space 按钮,弹簧返回一定量,同时称为 power 的变量每 Time.deltaTime(每秒)增加一定量,以计算弹簧返回球的速度。

在这个阶段,我有一个 Box Collider 从弹簧对象中出来,它的触发功能在其 Out 位置关闭,并在它回来击球时打开以避免任何对撞机故障。

当球回来时,它会激活对撞机,但对撞机不会推动球。我需要它以不同的力量推动球,具体取决于附在弹簧上的 Power 变量。这个项目的一个条件是我可能无法直接控制球,因此消除了 Rigidbody.AddForce 方法,因为对撞机必须给球提供动量。哦,两个物体都有刚体。

这是我写的脚本如果有人有任何想法或知道如何解决我遇到的问题,请在下面留下答案或评论。

    public int Points;
public GameObject Spring;
public float PowerPerSecond; // Increment To Increase Power Every Second When Spring Is Being Compressed, this allows different sensitivities of spring for different levels.
    void Start () 
    {
        // When Game Starts reset these values.
        SpringPosition = "Spring Is In Out Position";
        SpringStatus = 1;
        Power = 0f;
        Points = 0;
        Spring = GameObject.Find("Spring");
        Spring.collider.isTrigger = true;
    }

    void Update ()
    {
        //Report Error To Console if PowerPerSecond is At or Below A Value Of Zero
        if (PowerPerSecond<0)
        {
            Debug.LogError("POWER PER SECOND VARIABLE CANNOT BE A NEGATIVE NUMBER!");

        }
        // If spring is in out position & user holds down the Space Button, Start Compressing the spring.
        if (SpringStatus == 1 && Input.GetKey(KeyCode.Space))
           {
            SpringGettingCompressed();
           }

        if (Input.GetKeyUp(KeyCode.Space) && SpringStatus == 1)
        {
            SpringReleased();
        }


    }


    void SpringGettingCompressed()
    {
        if (transform.position.x > -2.3)
        {
        transform.position = new Vector3 (transform.position.x - 0.05f * Time.deltaTime,transform.position.y,transform.position.z);
        Power += PowerPerSecond * Time.deltaTime;

    }
}

    void SpringReleased()
    {
        if (transform.position.x <= -1.9){
        transform.position = new Vector3 (transform.position.x + Power * Time.deltaTime, transform.position.y, transform.position.z);
        SpringStatus = 2;
            Spring.collider.isTrigger=false;
        }
    }
}
4

1 回答 1

1

您需要使用弹簧的刚体而不是变换。即使你不能直接操纵球的刚体,你仍然可以在弹簧上使用rigidbody.AddForce。通过直接更改 transform.position ,您将绕过统一物理引擎。在做与物理相关的事情时,也使用 FixedUpdate() 而不是 Update()。

于 2014-05-31T12:18:09.687 回答