简体   繁体   English

如何在 Unity 中访问儿童 object 中的车轮对撞机

[英]How to access wheel collider in a child object in Unity

In Unity, my player gameObject has 4 wheels and all of them contain wheel colliders.在 Unity 中,我的玩家游戏对象有 4 个轮子,它们都包含轮子对撞机。 I want to get the rpm of these.我想得到这些的rpm。

I write this code, but at line 33 it is throwing index/argument out of bound exception, which means the index it's trying to access with 'GetChild(i)' is not accessible.我编写了这段代码,但在第 33 行它抛出了索引/参数超出范围异常,这意味着它试图使用“GetChild(i)”访问的索引不可访问。 line 33 is第 33 行是

 wColliders[i] = gameObject.transform.GetChild(i).gameObject.GetComponent<WheelCollider>();

My player has 5 child objects and 4 of them have a wheel collider.我的玩家有 5 个子对象,其中 4 个有轮子对撞机。 how to solve it?如何解决?

```
public class PlayerController : MonoBehaviour
 {

    //[SerializeField] private float speed = 10f;
    [SerializeField] private float speed;
    [SerializeField] private float rpm;
    private float totalRpm;
    [SerializeField] private float horsePower;
    [SerializeField] private float turnSpeed;
     public float horizontalInput;
     public float forwardInput;

    [SerializeField] private GameObject centerOfMass;
    [SerializeField] TextMeshProUGUI speedometerText;
    [SerializeField] TextMeshProUGUI rpmText;
    
    List<WheelCollider> wColliders;
    private Rigidbody playerRb;
    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody>();

        playerRb.centerOfMass = centerOfMass.transform.position;

        wColliders = new List<WheelCollider>();
        for(int i = 0; i < 4; i++)
        {
            wColliders[i] = gameObject.transform.GetChild(i).gameObject.GetComponent<WheelCollider>();
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        forwardInput = Input.GetAxis("Vertical");

        playerRb.AddRelativeForce(Vector3.forward * horsePower * forwardInput);
       
        transform.Rotate(Vector3.up, Time.deltaTime * turnSpeed * horizontalInput);

        speed = playerRb.velocity.magnitude;
        speed = Mathf.RoundToInt(speed);
        speedometerText.SetText("Speed: " + speed);

        for(int i = 0; i < wColliders.Count; i++)
        {
            totalRpm += wColliders[i].rpm;
        }

        rpm = Mathf.Round(totalRpm / wColliders.Count);

        rpmText.SetText("RPM: " + rpm);
    }
}

You get the IndexOutOfBoundsException not because Unity doesn't find the child but rather because you try to access/set您得到IndexOutOfBoundsException不是因为 Unity 没有找到孩子,而是因为您尝试访问/设置

wColliders[i]

of an empty list.一个列表。 There are no items added yet so you can not access them via the index.尚未添加任何项目,因此您无法通过索引访问它们。


You would rather use List<T>.Add您宁愿使用List<T>.Add

for(var i = 0; i < 4; i++)
{
    wColliders.Add(transform.GetChild(i).GetComponent<WheelCollider>());
}

Or actually rather simply use GetComponentsInChildren或者实际上更简单地使用GetComponentsInChildren

wColliders = GetComponentsInChildren<WheelCollider>(true).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM