简体   繁体   中英

How to rotate multiple RigidBodies in a prefab based on user input

I'm currently trying to rotate different Rigidbody s in a prefab based on separate inputs. I have it working when each Rigidbody has a separate script attached to it with the designated input, but I need to combine them into one "master control" script so I can eventually split the player character in half vertically so 1 player can control the left half and 1 player can control the right half of the body's limbs within the Mirror Networking API.

The working code is only attached to a single limb, in this case, the left bicep. The only thing that changes between limbs is the keyboard input and the multiplier variable.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LeftBicep : MonoBehaviour
{
    public float amount = 6000f;
    protected Rigidbody rb;
    public float multiplier = 4f;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float h = Input.GetAxis("leftbicep") * amount * Time.deltaTime;

        GetComponent<Rigidbody>().AddTorque(Vector3.right * h * multiplier); 
    }
}

And here's the "master control" script that is attached to the full body prefab that I can't get to work.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public Rigidbody forearmL;
    public Rigidbody forearmR;
    public Rigidbody shoulderL;
    public Rigidbody shoulderR;
    public Rigidbody thighL;
    public Rigidbody thighR;
    public Rigidbody legL;
    public Rigidbody legR;

    public float amount = 6000f;
    public float multiplier = 4f;

    // Start is called before the first frame update
    void Start()
    {
        forearmL = (Rigidbody)GetComponent("LeftForearm2");
        forearmR = (Rigidbody)GetComponent("RightForearm2");
        shoulderL = (Rigidbody)GetComponent("LeftBicep2");
        shoulderR = (Rigidbody)GetComponent("RightBicep2");
        thighL = (Rigidbody)GetComponent("LeftThigh2");
        thighR = (Rigidbody)GetComponent("RightThigh2");
        legL = (Rigidbody)GetComponent("LeftLeg2");
        legR = (Rigidbody)GetComponent("RightLeg2");
        
    }
    
    void FixedUpdate()
    {
            float lBicep = Input.GetAxis("leftbicep") * amount * Time.deltaTime;
            shoulderL.AddTorque(Vector3.right * lBicep * multiplier);

            float lFArm = Input.GetAxis("leftforearm") * amount * Time.deltaTime;
            forearmL.AddTorque(Vector3.right * lFArm * multiplier);
            
            float lThigh = Input.GetAxis("leftthigh") * amount * Time.deltaTime;
            thighL.AddTorque(Vector3.right * lThigh * multiplier);
            
            float lLeg = Input.GetAxis("leftleg") * amount * Time.deltaTime;
            legL.AddTorque(Vector3.right * lLeg * multiplier);

            float rBicep = Input.GetAxis("rightbicep") * amount * Time.deltaTime;
            shoulderR.AddTorque(Vector3.right * rBicep * multiplier);
            
            float rFArm = Input.GetAxis("rightforearm") * amount * Time.deltaTime;
            forearmR.AddTorque(Vector3.right * rFArm * multiplier);
            
            float rThigh = Input.GetAxis("rightthigh") * amount * Time.deltaTime;
            thighR.AddTorque(Vector3.right * rThigh * multiplier);
            
            float rLeg = Input.GetAxis("rightleg") * amount * Time.deltaTime;
            legR.AddTorque(Vector3.right * rLeg * multiplier);

            
    }
}

The exception I get when running the game with the player prefab that has the "master control" script in the scene is

NullReferenceException: Object reference not set to an instance of an object
Movement.FixedUpdate () (at Assets/Scripts/test/Movement.cs:38)

How do I go about getting this to work?

I'm pretty sure your issue is that all of these

forearmL = (Rigidbody)GetComponent("LeftForearm2");
forearmR = (Rigidbody)GetComponent("RightForearm2");
shoulderL = (Rigidbody)GetComponent("LeftBicep2");
shoulderR = (Rigidbody)GetComponent("RightBicep2");
thighL = (Rigidbody)GetComponent("LeftThigh2");
thighR = (Rigidbody)GetComponent("RightThigh2");
legL = (Rigidbody)GetComponent("LeftLeg2");
legR = (Rigidbody)GetComponent("RightLeg2");

will return null since the component you are looking for is called Rigidbody .. not LeftForearm2 etc. see GetComponent(string type)

type
The type of Component to retrieve.


Just make all references in the exposed fields via the Unity Inspector and remove your Start method entirely!

Or alternatively it sounds like what you passed into the GetComponent is actually the name of your child objects nested directly under the main controller so you could do (but I wouldn't as said)

// See https://docs.unity3d.com/ScriptReference/Transform.Find.html
forearmL = transform.Find("LeftForearm2").GetComponent<Rigidbody>();
forearmR = transform.Find("RightForearm2").GetComponent<Rigidbody>();
shoulderL = transform.Find("LeftBicep2").GetComponent<Rigidbody>();
shoulderR = transform.Find("RightBicep2").GetComponent<Rigidbody>();
thighL = transform.Find("LeftThigh2").GetComponent<Rigidbody>();
thighR = transform.Find("RightThigh2").GetComponent<Rigidbody>();
legL = transform.Find("LeftLeg2").GetComponent<Rigidbody>();
legR = transform.Find("RightLeg2").GetComponent<Rigidbody>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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