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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Movement in the current update

Discussion in 'Getting Started' started by InsensitveJ0ker, Jul 1, 2019.

  1. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    Hey, I've been looking through tutorials but they all seem to be out of date in terms of 3d character movement. So I've been looking for the specific word to make my character move, but I can't find it. Got any vocab I could use?
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    656
    What?
     
  3. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    uh..... i dont really use 3d, but transform.translate, Vector3, and addforce might help
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Well I started with Unity back in Unity 4, and movement worked the same back then as it does now, so you must be using some really old tutorials. Take a look at the Learn section for something more recent, or find some YouTube tutorials which were posted in the last few years.
     
  5. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    Aight hold up. How would you use addforce? Like would you go "rb.AddForce()" or something else because doing that in the quotes doesn't seem to match up with the compiler.
     
  6. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    rb.AddForce is how you do it. However, it does not take in a single value, and cannot just be called as is.
    What I mean is it has to be used like
    float thrust = 100;
    rb.AddForce(transform.forward * thrust);
     
  7. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    are you using vs 2017 or 2019
     
  8. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    It just that there something I'm missing in this.
     

    Attached Files:

  9. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    258
    This is the code you currently have:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Movement : MonoBehaviour
    4. {
    5.     public float gasForce = 500f;
    6.     public float rearForce = 50f;
    7.     public float steeringForce = 250f;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void FixedUpdate()
    16.     {
    17.         if(Input.GetKey("w"))
    18.         {
    19.             rb.AddForce(0, gasForce * Time.deltaTime, 0);    
    20.         }
    21.         if(Input.GetKey("s"))
    22.         {
    23.             rb.AddForce(0, -rearForce * Time.deltaTime, 0);
    24.         }
    25.         if(Input.GetKey("a"))
    26.         {
    27.             rb.AddForce(steeringForce * Time.deltaTime, 0, 0);
    28.         }
    29.         if(Input.GetKey("d"))
    30.         {
    31.             rb.AddForce(-steeringForce * Time.deltaTime, 0, 0);
    32.         }
    33.     }
    34. }
    In the future please post your code like this.

    What you are missing here is a definition for what rb is.
    Assuming you have a RigidBody on your GameObject, you can get a reference to that RigidBody using the GetComponent method that MonoBehaviour provides you.

    To save performance, because we want all of those sweet fps, you would normally only ask for a component once and remember the result. That is most often done in the Start() method.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Movement : MonoBehaviour
    4. {
    5.     public float gasForce = 500f;
    6.     public float rearForce = 50f;
    7.     public float steeringForce = 250f;
    8.     // Make it private, so it can't be messed with in the Inspector.
    9.     private RigidBody rb;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.          // Note that this will fail horribly if you don't have a RigidBody component on you GameObject
    14.          rb = GetComponent<RigidBody>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void FixedUpdate()
    19.     {
    20.         if(Input.GetKey("w"))
    21.         {
    22.             rb.AddForce(0, gasForce * Time.deltaTime, 0);      
    23.         }
    24.         if(Input.GetKey("s"))
    25.         {
    26.             rb.AddForce(0, -rearForce * Time.deltaTime, 0);
    27.         }
    28.         if(Input.GetKey("a"))
    29.         {
    30.             rb.AddForce(steeringForce * Time.deltaTime, 0, 0);
    31.         }
    32.         if(Input.GetKey("d"))
    33.         {
    34.             rb.AddForce(-steeringForce * Time.deltaTime, 0, 0);
    35.         }
    36.     }
    37. }
    If you want to make sure that Unity tells you if you are missing that RigidBody on your GameObject, you can use RequireComponent. I actually highly recommend to do that, because you can be sure that you (also I and everyone else here) will make every stupid mistake thats possible :D
     
    Green11001 likes this.
  10. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    Thanks you so very much! That's quite reassuring.