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

Moving the player in the unity tutorial

Discussion in 'Scripting' started by sangarinos, Feb 3, 2015.

  1. sangarinos

    sangarinos

    Joined:
    Feb 3, 2015
    Posts:
    5
    Hello!
    I'm new in this whole Unity thing and wanted to learn sth from the tutorials. Started with the project called Roll-a-Boll. The idea is simple, move the sphere around the plane with the input from keyboard. I did everything exactly like in the tutorial, my script looks exactly the same, though when it comes to testing and trying to move the ball around, it's not working at all while in the tutorial it's rolling around. Again, I'm totally knew here, like "downloaded Unity today and turned it on for the first time about 1,5h ago" new and it frustrates me that I have absolutely no idea why it's not working. Any chance someone, anyone can help?
    Here's this little script in C#:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.     void FixUpdate()
    9.     {
    10.         float moveHorizontal = Input.GetAxis ("Horizontal");
    11.         float moveVertical = Input.GetAxis ("Vertical");
    12.  
    13.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    14.  
    15.         rigidbody.AddForce (movement * speed);
    16.     }
    17. }
    18.  
     
  2. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    Try and go to the menu bar and choose Edit -> Project Settings -> Input, where you sould find a "Horizontal" and "Vertical" value in the inspector, try to expand them and check "positive" and "negative" buttons (it should be arrow keys by default) also it should be "Key or Mouse Button" in type per default.

    Obviously you attached the script to the ball? ;) im only asking since you said you started a few hours ago! :)
     
  3. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    I had this exact problem But i have been using unity for a couple of months I attached the script to the ball as well. (I try what you said)
     
  4. sangarinos

    sangarinos

    Joined:
    Feb 3, 2015
    Posts:
    5
    First of all, Ramsdal, thanks for the quick answer :)

    Yes, I did attach it to the ball in the beginning, they even said to do so in the tutorial ;)

    The interesting thing is that the Horizontal and Vertical values appear twice in this inspector o_O When I expand them, those first H. and V. have everything set as you said they should have, but the other H. and V. are basically blank. Do you think that this might cause a problem?
     
  5. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    this is what my input setting look like i think that it is right if not please say @Ramsdal, also @sangarinos is this kinda what yours looks like?
     
  6. sangarinos

    sangarinos

    Joined:
    Feb 3, 2015
    Posts:
    5
    Yes, it looks exactly the same. Though, dunno about you, @NintendoMaster00, but in my case there's another Horizontal and Vertical thingy a bit lower on the list and it looks like this:

    upload_2015-2-3_21-17-17.png
     
  7. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    That looks right... The first two horizontal/vertical is for mouse and keyboard the last is for joystick.

    Very strange, aslo you have a rigidbody component attached to the ball?
     
  8. sangarinos

    sangarinos

    Joined:
    Feb 3, 2015
    Posts:
    5
    I do, yes. Here you have it, together with the script attached:

    upload_2015-2-3_21-20-27.png

    It's really odd, cause I swear I did everything just like in the tutorial and it works there while in my editor it doesn't...
     
  9. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    yep but for joystick
    yes maybe it will be fixed later in the tutorial
    mine looks the same as that.
     
  10. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    Also this line:
    Code (CSharp):
    1. rigidbody.AddForce (movement * speed);
    Should be:
    Code (CSharp):
    1. rigidbody.AddForce (movement * speed * Time.deltaTime);
     
  11. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    Hehe spottet it, it is FixedUpdate not FixUpdate ;) cannot see the forrest for trees!
     
    NintendoMaster00 likes this.
  12. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControler : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.     void fixedupdate ()
    9.     {
    10.         float moveHorizontal = Input.GetAxis ("Horizontal");
    11.         float moveVertical = Input.GetAxis("Vertical");
    12.  
    13.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    14.  
    15.         rigidbody.AddForce(movement * speed * Time.deltaTime);
    16.     }
    17. }
    18.  
    this is my code for the player controller so far.

    thanks for the help @Ramsdal

    my fixed code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControler : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.     void FixedUpdate ()
    9.     {
    10.         float moveHorizontal = Input.GetAxis ("Horizontal");
    11.         float moveVertical = Input.GetAxis("Vertical");
    12.  
    13.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    14.  
    15.         rigidbody.AddForce(movement * speed * Time.deltaTime);
    16.     }
    17. }
     
  13. sangarinos

    sangarinos

    Joined:
    Feb 3, 2015
    Posts:
    5
    Oh come on, you've got to be kitting me! How could I not notice it? xD Yes, it works perfectly well now, thank you very much for your help @Ramsdal :D
     
  14. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    It is case sensative so it needs to be FixedUpdate so the final code would be:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RollerTest : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     void FixedUpdate()
    8.     {
    9.         float moveHorizontal = Input.GetAxis ("Horizontal");
    10.         float moveVertical = Input.GetAxis ("Vertical");
    11.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    12.         rigidbody.AddForce (movement * speed * Time.deltaTime);
    13.     }
    14.  
    15. }
     
    NintendoMaster00 likes this.
  15. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    My problem was actually that i did not capitalize any of fixedupdate and i had a space between vector 3 and the ()

    your post helped me find the error though thanks again :D
     
    Ramsdal likes this.
  16. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    Your welcome, and as I just proved - it is easy to stare yourself blind on a problem ;)
     
  17. gerdw

    gerdw

    Joined:
    Mar 13, 2015
    Posts:
    8
    I'm having a problem with the same script, because I am using Unity 5, and the last line changes to:
    GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);

    Unfortunately, now the ball starts moving up and down...I have no clue why that happens. Anyone an idea?

    Thanks!
     
  18. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    In any case the case the call to get the Rigidbody should not be in the update method, i would place in the start function i think:
    Code (CSharp):
    1. public Rigidbody rb;
    2.  
    3. void Start()
    4. {
    5.    rb = GetComponent<Rigidbody>();
    6. }
    7.  
    and then use:
    Code (CSharp):
    1. rb.AddForce (movement * speed * Time.deltaTime);
    If the problem persists try to paste the whole script :)
     
  19. gerdw

    gerdw

    Joined:
    Mar 13, 2015
    Posts:
    8
    Thanks. This is what I have now, but doesn't work.. As you can see: newbie :)


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.    
    4. public class PlayerController : MonoBehaviour {
    5.        
    6.     public float speed;
    7.     public Rigidbody rb;
    8.    
    9.     void Start()
    10.     {
    11.         rb = GetComponent<Rigidbody>();
    12.     }
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         float moveHorizontal = Input.GetAxis ("Horizontal");
    17.         float moveVertical = Input.GetAxis ("Vertical");
    18.  
    19.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    20.  
    21.         rb.AddForce (movement * speed * Time.deltaTime);    }
    22. {
     
  20. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    I am guessing the last line is just a mistake right? (line 22 the bracket is turning the wrong way)

    And you have added the rigidbody to the ball like in post 8?
     
  21. gerdw

    gerdw

    Joined:
    Mar 13, 2015
    Posts:
    8
    Yeah line 22 was mistake, but was not the real problem. I did add the rigidbody to the ball, but the ball keeps moving up and down without doing anything with my keys...
    As you can see in printscreen, the position Y and Z, and the rotation X are changing, while they should be fixed (when I don't used the keys).

    upload_2015-3-13_15-56-54.png
     
  22. gerdw

    gerdw

    Joined:
    Mar 13, 2015
    Posts:
    8
    Code (CSharp):
    1.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    2.  
    3.         if (movement != Vector3.zero) {
    4.             rb.AddForce (movement * speed * Time.deltaTime);
    5.         }
    I slightly changed the code, but still a moving ball