物理问题团结3d

时间:2014-05-28 07:10:52

标签: c# unity3d physics

我的团结游戏是一个春天,当你握住空格键时,弹簧会向后移动。释放空格键时,弹簧会释放,具体取决于按住空格键时产生的扭矩量。

当它被释放时,弹簧刚刚重叠在球体内部,我需要球更快或更慢地向前移动,这取决于Power变量中的功率量。

我不能使用Rigidbody.Addforce,因为弹簧需要让球移动。

脚本注释:

- >如果弹簧完全伸出(释放),则其值应为-2如果保持在其最大扭矩,则其x轴值应为-2.2


using UnityEngine;
using System.Collections;

public class SpringMechanics : MonoBehaviour {

public bool SpringIn;
public float Power;
public bool SpringReleased;
void Update()
{
    // IF THE SPRINGS X POSITION IS AT -2 OR ABOVE THAT VALUE IT WILL MARK THE SPRING IN VARIBALE AS TRUE

    if (transform.position.x >=-0.5f)
    {
        SpringIn = false;

    }
    else
    {
        SpringIn = true;
    }

    //CHECKS IF MOUSE SPACE BUTTON IS DOWN AND IF SO MOVES SPRING IN

    if (Input.GetKey (KeyCode.Space) && transform.position.x <= 2.3) {
                    Debug.Log ("Spring Being Compressed");

                    // Moves Spring Backwards when space key is down

        //rigidbody.velocity = new Vector3(rigidbody.velocity.x - 0.05f * Time.deltaTime,rigidbody.velocity.y,rigidbody.velocity.z);
                            transform.position = (new Vector3 (transform.position.x - 0.05f * Time.deltaTime, transform.position.y, transform.position.z));
                            // Adds 3 Units Of Power Every Second While Key Is Down
                            Power += 2 * Time.deltaTime;

            }
    //Releases Ball When Space Bar Is Up After Being Down

    if (Input.GetKeyUp (KeyCode.Space))
    {
        SpringReleased = true;
    }

    if (SpringReleased == true) 
    {
        FireBall();

    }
}

//When Firing Ball Executer Below Function
void FireBall()
{
    if (transform.position.x <= -2){
        rigidbody.velocity = new Vector3(rigidbody.velocity.x + Power * Time.deltaTime,rigidbody.velocity.y,rigidbody.velocity.z);
        //transform.position = (new Vector3 (transform.position.x + Power * Time.deltaTime, transform.position.y, transform.position.z));
        transform.position = (new Vector3 (-2, transform.position.y, transform.position.z));

        if (transform.position.x >= -2 && SpringReleased == true){
            SpringReleased=false;
            Power = 0;
            rigidbody.velocity = new Vector3(0,0,0);
            Debug.Log ("Reset Spring");

            }
        }   
    }
}

如何根据功率变量的值使用弹簧向前击中?

1 个答案:

答案 0 :(得分:0)

将力施加到弹簧(或弹簧的某些部分,如顶部的单独网格),而物理引擎应该完成其余的工作。