S
S
Senture2018-03-26 15:42:03
Unity
Senture, 2018-03-26 15:42:03

Object not moving in unity?

Hello, I copied a script from one Internet resource for managing a sphere in unity. Here is the code:

void FixedUpdate() {
        if ( netView.isMine ) { // если обькт принадлежит нам то мы им управляем иначе делаем интерполяцию движения
            float inputX = Input.GetAxis( "Horizontal" );
            float inputY = Input.GetAxis( "Vertical" );

      if ( inputX != 0.0f ) {
                rigidbody.AddTorque( Vector3.forward * -inputX * power, ForceMode.Impulse );
            }
            if ( inputY != 0.0f ) {
                rigidbody.AddTorque( Vector3.right * inputY * power, ForceMode.Impulse );
            }
        } else {
      syncTime += Time.fixedDeltaTime;
            rigidbody.position = Vector3.Lerp( syncStartPosition, syncEndPosition, syncTime / syncDelay ); // интерполяция перемещения
            rigidbody.rotation = Quaternion.Lerp( syncStartRotation, syncEndRotation, syncTime / syncDelay ); // интерполяция поворота
    }
  }

When you click on any of the movement buttons, it gives the following error:
NullReferenceException
UnityEngine.Rigidbody.AddTorque (Vector3 torque, ForceMode mode) (at C:/buildslave/unity/build/artifacts/generated/common/modules/Physics/DynamicsBindings.gen .cs:1466)
PlayerControls.FixedUpdate () (at Assets/PlayerControls.cs:42)
PS Thanks for the help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Simonov, 2018-03-26
@Senture

You also need to get the rigidbody via GetComponent

public float torque;
    public Rigidbody rb;

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

    void FixedUpdate()
    {
        float turn = Input.GetAxis("Horizontal");
        rb.AddTorque(transform.up * torque * turn);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question