函数无能为力

时间:2019-01-24 18:11:30

标签: function unity3d

我有一个名为BounceBack的游戏对象,当它们碰撞在一起时,应该将球弹回很远的地方。

public class BounceBack : MonoBehaviour
{
    public GameObject Player;
    public float force;

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag(Player.tag))
        {
            Player.GetComponent<PlayerController>().ForceBack(force);
        }
    }
}

ball player(球)脚本:

public class PlayerController : MonoBehaviour
{
    public int acceleration;
    public int speedLimit;
    public int sideSpeed;

    public Text countText;
    public Text winText;

    public GameObject pickUp;
    public GameObject finishLine;

    //internal void ForceBack() //Not sure what it does and why it's there.
    //{
    //    throw new NotImplementedException();
    //}

    private int count;
    private Rigidbody rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        SetCount();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal") * sideSpeed * rb.velocity.magnitude / acceleration;
        //float moveVertical = Input.GetAxis("Vertical") * acceleration;

        if (rb.velocity.magnitude <= speedLimit)
        {
            rb.AddForce(0.0f, 0.0f, acceleration); // add vertical force
        }
        rb.AddForce(moveHorizontal, 0.0f, 0.0f); // add horizontal force
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag(pickUp.tag))
        {
            other.GetComponent<Rotate>().Disapear();
            count++;
            SetCount();
        }
        if (other.gameObject.CompareTag(finishLine.tag))
        {
            acceleration = 0;
            sideSpeed = 0;
            finishLine.GetComponent<GameEnd>().FadeOut();
            if (count >= 2)
            {
                winText.GetComponent<WinTextFadeIn>().FadeIn("Vous avez remporté la partie!");
            }
            else
            {
                winText.GetComponent<WinTextFadeIn>().FadeIn("Vous avez perdu. Réesayer?");
            }
        }
    }

    private void SetCount()
    {
        countText.text = "Count : " + count.ToString();
    }

    public void ForceBack(float force)
    {
        Rigidbody rb = GetComponent<Rigidbody>();
        rb.AddForce(0.0f, 0.0f, -force, ForceMode.VelocityChange);
        Debug.Log("Pass");
    }
}

AddForce 功能不执行任何操作。我尝试了 setActive(false),但它也不起作用。唯一有效的方法是 Debug.Log()。我不确定速度限制和加速度是否会干扰该功能。

编辑:我不确定问题是否来自Unity,但是我无法从类内部的forceBack函数访问该类的任何变量。

EDIT2:我也尝试直接在Bounce Back脚本中调用AddForce函数,但是它也不起作用。

Player.GetComponent<Rigidbody>().AddForce(0.0f, 0.0f, -force, ForceMode.VelocityChange);

Player (Ball) Screenshot

Bounce Back Screenshot

1 个答案:

答案 0 :(得分:1)

几件事:

1。)如果正确设置了对撞机和刚体,则物理系统应该已经使球反弹。如果球在反弹时应该获得动力,则只需要执行以下操作即可。如果此答案无济于事,则应发布其检查员的屏幕截图。

2。)在rb.AddForce()调用中,您正在世界空间中施加力,这可能是错误的反弹方向。如果您知道球是按照其移动方式定向的,则可以使用相同的参数调用AddRelativeForce。如果没有控制球的方向,则需要在施加力之前计算出要使用的正确的世界空间方向。

3。)最后,只是为了确认一下,附加了BounceBack的对象的确在检查器的'force'参数中具有非零值,对吧?