【问题标题】:Functions not doing anything函数什么都不做
【发布时间】:2019-01-25 04:13:40
【问题描述】:

我有一个名为 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

【问题讨论】:

  • 您是否尝试过使用物理材料来确定物体应该如何反弹?
  • 您的 PlayerController 脚本在检查器中具有“速度限制”和“加速度”属性,这让我相信它正在处理运动。它是否在做任何可能干扰设置刚体速度的事情?
  • 是的,但弹跳不够
  • 我就是这么想的。我发布了整个代码

标签: function unity3d


【解决方案1】:

所以,有几件事:

1.) 如果您正确设置了对撞机和刚体,物理系统应该已经使球反弹。如果球在反弹时应该获得动量,你应该只需要做这样的事情,这是不太可能的。如果这个答案没有帮助,您应该发布他们检查员的屏幕截图。

2.) 在您的 rb.AddForce() 调用中,您在世界空间中施加了一个力,这可能是错误的反弹方向。如果您知道球的方向与它的移动方式相同,那么您可以使用相同的参数调用 AddRelativeForce。如果球的方向不受控制,那么您需要在施加力之前计算正确的世界空间方向以使用。

3.) 最后,为了确认一下,附加了 BounceBack 的对象在检查器的“force”参数中确实有一个非零值,对吧?

【讨论】:

  • 1)如果你说的是物理材质,我已经应用了一个,但是弹性不够高。 2)感谢您的通知 3)是的,我尝试了每个值,也在检查员中
  • 好的,请发布播放器对象和反弹对象之一的检查器面板的屏幕截图。
  • 我上传了截图
  • 嗯...我在所提供的内容中看不到任何会导致问题的内容。您可以尝试删除任何自定义物理材料吗?它们是我在上面根本看不到的少数东西之一。
猜你喜欢
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多