Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Player movement

Discussion in 'Physics' started by Aloisius2008, Feb 5, 2023.

  1. Aloisius2008

    Aloisius2008

    Joined:
    Feb 5, 2023
    Posts:
    1
    I wrote the following string:

    void FixedUpdate()
    {
    rb.AddForce(0, 0, 500 * Time.deltaTime);
    }

    but eventhough I checked different tutorials the player doesn't move.

    What's wrong? What I've to check?

    Thnks a lot
     
  2. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    Doesn't look like you assigned rb to anything, look at this example,

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Example : MonoBehaviour
    4. {
    5.     Rigidbody m_Rigidbody;
    6.     public float m_Thrust = 20f;
    7.  
    8.     void Start()
    9.     {
    10.         //Fetch the Rigidbody from the GameObject with this script attached
    11.         m_Rigidbody = GetComponent<Rigidbody>();
    12.     }
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         if (Input.GetButton("Jump"))
    17.         {
    18.             //Apply a force to this Rigidbody in direction of this GameObjects up axis
    19.             m_Rigidbody.AddForce(transform.up * m_Thrust);
    20.         }
    21.     }
    22. }