Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Official Roll-a-ball Tutorial Q&A

Discussion in 'Community Learning & Teaching' started by Adam-Buckner, Apr 17, 2015.

  1. Modder Keisalex

    Modder Keisalex

    Joined:
    Oct 6, 2015
    Posts:
    1
    Hey guys, i'm also having problem with moving the ball. I got to the next video, the one in which i made the camera move around. I managed to do it right, but stopped there, as i still can't find a way to move the ball.. :(

    Below is my code:

    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

    public float speed;

    private Rigidbody rb;

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

    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    rb.AddForce (movement * speed);
    }
    }


    Any tips? I really want to get started in this, but i'm a complete newbie, so any help provided will be massively appreciated.

    EDIT: Also, the programmer/narrator in the video says, and shows, that after creating a public variable, i'll be able to edit it in the editor, but there is no speed variable appearing in the inspector of the player object. :confused:
     
    Last edited: Oct 6, 2015
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening,

    What I would try first is to check the placement of your win text, and ensure you can see it.
    within your Start() function of the PlayerController.
    you will have
    Code (CSharp):
    1. winText.text = "";
    try changing this temporarily to show something just to check that you can actually see it on screen, to something like
    Code (CSharp):
    1. winText.text = "Win Text Here";
    if the placements ok, check your SetCountText() function, and pop in a little debug line to show your counter in the console window.
    Code (CSharp):
    1.  void SetCountText ()
    2.     {
    3.         countText.text = "Count: " + count.ToString ();
    4.        // just a test debug line
    5.       Debug.Log("Count : " + count);
    6.         if (count >= 12)
    7.         {
    8.             winText.text = "You Win!";
    9.         }
    10.     }
    make sure your comparison >= is the same, and there are the same amount of pickups in the game as you have in this script, in this case 12.

    also, just to ensure that the pickups are being registered as being collected, pop another debug line in the OnTriggerEnter function, just before setactive(false)
    Code (CSharp):
    1. Debug.Log("Pickup collected"); // pop this in just to check
    2.             other.gameObject.SetActive(false);
    and finally, ensure that the tag on the pickup object is exactly the same in spelling/spacing and case as what you have in this function also.


    see how that goes, sorry for the length of post :( a few things that I would check to find out where things could be going funky :)
     
    Last edited: Oct 6, 2015
    Krossover likes this.
  3. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    in the console window, are you getting any errors or warnings showing at all?

    First glance seems ok. firstly the Player object you have in your scene. ensure that this script is attached to that object.
    if highlighting the Player ball, and looking in the inspector you still cant see the public speed variable, I would remove and re-add the PlayerController script to the Player object and see if that works.
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    @OboShape has given a through answer (THANKS!). Let us know if you are still having issues.
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please learn to use code tag properly?

    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Another great reply by @OboShape !! Let us know if you're still having issues.
     
  7. AllTheGoodNamesWereTaken

    AllTheGoodNamesWereTaken

    Joined:
    Sep 24, 2015
    Posts:
    14
    Thank you for the great tutorial.
     
  8. Nezfen

    Nezfen

    Joined:
    Oct 13, 2015
    Posts:
    1
    Just wanted to leave a thank you message. This was essentially my introduction to programming and game development, and I had a great time doing it. The guide was excellent, very informative and easy to follow.
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You're welcome!

    We always appreciate the feedback, and it's great to know that we can be helpful!

    If you ever have any questions, just let us know.
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  11. ishpreet.1990

    ishpreet.1990

    Joined:
    Oct 13, 2015
    Posts:
    1
    Hi, I am learning unitychan and after writing scripts the doll is only moving the eyes not the other things. I have checked all the steps twice. but still it is same. What should i do? I have one error unassigned exception error.
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Sadly, this isn't the correct place to ask about unity chan. This is a support thread for the roll-a-ball project. Can you please post in a more appropriate section?
     
  13. GameplayStudent

    GameplayStudent

    Joined:
    Oct 14, 2015
    Posts:
    5
    Hey, I'm also having a problem getting my ball to move. It won't move in any direction, and I finished the second video so it should work. I did go in Unity and set speed equal to 100, but its not working. Help
    Problem 1.1.PNG
     
  14. GameplayStudent

    GameplayStudent

    Joined:
    Oct 14, 2015
    Posts:
    5
  15. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Good evening @Batman_nerd,

    Looking at your player controller script. Its an easy one to miss, but your Start method should have a capital S at the beginning .

    It is an easy one to miss as Unity will not throw an error based on the case of the method, it just wont call it.

    In our case here there was an error which applies to it trying to use something that hasn't been set. This is pointing towards the line rb.AddForce... Due to rb not being set since our Start method was not called.

    Change the Start() to upper case S and you should be good to whizz around :)

    [apologies for the less than eloquent explanations, only got access to my phone at present]
     
    Adam-Buckner likes this.
  16. GameplayStudent

    GameplayStudent

    Joined:
    Oct 14, 2015
    Posts:
    5
  17. GameplayStudent

    GameplayStudent

    Joined:
    Oct 14, 2015
    Posts:
    5
    Hey, so now I'm having a problem trying to get the camera to not roll with the ball. I finished the video, and I checked capitalization. I don't have any errors, and I don't know what to do. Problem 2.1.PNG Prboelm 2.2.PNG
     
  18. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning,

    I see the camera is childed to the Player.
    If memory serves this was done by Adam in the video to illustrate a 'What if.' scenario to show how this wont work.
    Later in the video the camera is put back to root level of the heirarchy (on its own and not childed).

    If things are childed to other gameobjects they also utilize their parents position and rotation as a reference point, but if on their own in the scene they us the world co-ordinates.

    If you put the main camera back to the top level of the heirarchy, leaving the player on its own.
    look at the camera in the inspector, drag the player from the heirarchy into the player slot on your camera script.

    should be good to rock-and-roll-a-ball :)

    You should find that when the game starts, each of the objects have a Transform.position, so when the camera script Starts, it finds the Players position and its own position and takes the difference to use as an offset for the camera.

    have another watch of the Camera Movement vid for more info.
    http://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-camera?playlist=17141
     
    Adam-Buckner likes this.
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    ^What he said!
     
  20. sdip

    sdip

    Joined:
    Oct 15, 2015
    Posts:
    1
    In the section "Collecting the Pick Up objects", the vanishes as soon as it touches any cube. How to fix this issue???
     
  21. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening,
    I'm guessing its the player that's vanishing, like in the question
    (sorry couldn't resist)

    Have a look again at your OnTriggerEnter method in the PlayerController script.

    And ensure it is the other game object you are setting inactive.
    Double check against Adams script.
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.gameObject.CompareTag ("Pick Up"))
    4.         {
    5.             other.gameObject.SetActive (false);
    6.         }
    7.     }
    as just setting
    Code (CSharp):
    1. gameObject.SetActive(false);
    Will deactivate the player, or more precisely the object this script is attached to.

    See how that goes :)
     
  22. Nightitan

    Nightitan

    Joined:
    Oct 17, 2015
    Posts:
    1
    i cant make the player move this is the script i used
    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

    public float speed;

    private Rigidbody rb;

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

    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    rb.AddForce (movement * speed);
    }
    }
    plz tell me if somethings wrong
     
    Last edited: Oct 17, 2015
  23. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning @Nightitan,
    (if you can please, when posting code, it does make it alot easier to read if you wrap it with code tags
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/)

    Have you set the speed value in the inspector?
    No errors showing in the console is there?
     
  24. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  25. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Hi there!I just started using unity and i was so excited to make a game on my one so i started watching this tutorial but suddenly i got a parsing error about the PlayerController C# code here it is : Assets/Scripts/PlayerController.cs(1,19): error CS8025: Parsing error

    And here is my code :
    Code (csharp):
    1.  using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5. 

public class PlayerController : MonoBehaviour {

  
    6.  
    7. public float speed;


    8.  
    9. private Rigidbody rb;


    10.  
    11. // Use this for initialization
  
    12.  
    13. void Start () {
    14. rb = GetComponent<Rigidbody>();
    15.   }

  
    16.  
    17. void FixedUpdate ()
  
    18. {
  
    19.   float moveHorizontal = Input.GetAxis("Horizontal");
  
    20.   float moveVertical = Input.GetAxis("Vertical");


    21.  
    22.   Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    23.  
    24. 

  rb.AddForce (movement * speed);
  }}
    Please help me i want to get back on the tutorial asap!
     
    Last edited: Oct 17, 2015
  26. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please fix your code? I can't read it. It's in one long line!
     
  27. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Sure here i fixed it!
     
  28. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Theres nothing I can see at first glance, I've copied the code and it built fine.


    I take it you have tried saving and closing Mono and Unity, restarting Unity and running again. or even removing and readding the script to the player?
     
  29. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    what's mono and yes i have already tried re-adding the script to the player + restarting unity but nothing however when i preview the code on unity when i click on it it looks different than the others it's like one straight line!
     
  30. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I don't see anything wrong here either. If oboshape can run it, then I don't understand why you can't.

    Delete the script and rewrite it? Copy and paste it into a new script?
     
  31. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    done that but nope nothing!
     
  32. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Nope nothing what? No errors? Can you copy and paste all of every error you have?
     
  33. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    i have re-written the code and paste it into a new script but nothing changed!
     
  34. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    WE FIXED IT!
     
  35. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    I re-wrote the code again in the MonoDevelop editor and assigned it again on the player and it worked! Thank you so much!
     
    OboShape likes this.
  36. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Cool Nice one. just ignore the last PM then lol

    [Notes : just in case anyone comes up against similar in future]

    OK, SP was using the VS editor as opposed to MonoDevelop.

    tried to copy, paste into notepad, copy that and repaste into editor to ensure straight ascii text.
    copy and pasting straight from VS to Mono script appeared to drop a couple of lines of code along the way for some reason, which would tie in towards why the parsing errors may have been present where it couldnt interpret that same individual lines that wouldnt paste correctly. correlating to the line number parse error in the original post.

    It would appear that there was some extra header info or strange formatting gremlins at work.
    Changing the default editor to MonoDevelop and recreating the script using Mono seemed to work a charm in this case.

    Hoorah!

    Good job bud for sticking with it :)
     
  37. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Thanks but now i have another problem however it does not show up as an error. In part 6/8 of this tutorial we make the player collide with the Pick up objects but when i run the game i cannot use my arrow keys or WASD keys to move the player.The player remains in his position.I thought it was a Player script error but i don't get any errors or warnings!
     
  38. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Have you set the speed value in the inspector?
     
  39. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    yeah it is set to 10!
     
  40. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Let us know if you find any particular issue that's stopping you from rolling the ball. I see the occasional note on YouTube, but I've never been able to reproduce this. I always assume there is some standard error, like: Not saving the script; Not setting the speed; Not attaching the script to the game object; Having any errors in the console preventing the game to run correctly; Not writing the correct code... Like having fixedUpdate instead of FixedUpdate...

    Can you paste your code again just to be clear?

    If it's none of the above, I'm curious what the issue is.
     
  41. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Ok here is my code and no i don;t get any console errors :
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.    public float speed;
    8.  
    9.    private Rigidbody rb;
    10.  
    11.    // Use this for initialization
    12.    void Start () {
    13.      rb = GetComponent<Rigidbody> ();
    14.    }
    15.    
    16.    void FixedUpdate () {
    17.      float moveHorizontal = Input.GetAxis ("Horizontal");
    18.      float moveVertical = Input.GetAxis ("Vertical");
    19.  
    20.      Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    21.    }
    22.  
    23.    void OnTriggerEnter(Collider other)
    24.    {
    25.      if (other.gameObject.CompareTag ("Pick Up"))
    26.      {
    27.        other.gameObject.SetActive(false);
    28.      }
    29.    }
    30. }
    31.  
     
  42. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    you have not applied the movement you worked out, need to add the rb.AddForce(movement * speed); within your FixedUpdate to apply it.
    see code below;
    Code (CSharp):
    1. void FixedUpdate ()
    2. {
    3.     float moveHorizontal = Input.GetAxis ("Horizontal");
    4.     float moveVertical = Input.GetAxis ("Vertical");
    5.    
    6.     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    7.  
    8.     // need this to apply the movement you have set above
    9.     rb.AddForce (movement * speed);
    10. }
     
  43. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Thank you so much, i totally forgot about it. Btw can i ask you something? Do you know any websites that can teach you c# ? and also is there any difference between c# and c++ when it comes to code?
     
  44. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    For learning C# there is a sticky in the Teaching forum section, worth a look through
    http://forum.unity3d.com/threads/getting-started-with-coding-for-unity.113937/

    Got to admit I've learned a shedload working through the tutorials, live sessions and projects. then tinker around with them afterwards with a "what if i change this.." kind of outlook, but thats just me :) and then if stuck or curious, look some things up in the Scripting reference or manual for info.

    There are some fundamental differences between them, bit off topic to discuss here though.
    https://en.wikibooks.org/wiki/C++_Programming/Programming_Languages/Comparisons/C_Sharp

    For Unity C# is the one used throughout the majority of tutorials that ive seen, and here on Unity.
     
    Last edited: Oct 18, 2015
  45. philosopherjeff

    philosopherjeff

    Joined:
    Oct 18, 2015
    Posts:
    3
    umm i encountered this problem, which is like I finished everything before adding text, i checked my code too, but the ball simply "passes" through the pickups, without the pickups disactivating. it's like they merge together. 屏幕快照 2015-10-18 下午11.31.41.png
     
  46. philosopherjeff

    philosopherjeff

    Joined:
    Oct 18, 2015
    Posts:
    3
    and here's my code 屏幕快照 2015-10-18 下午11.36.20.png
     
  47. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    onTriggerEnter is spelled incorrectly. Try OnTriggerEnter instead.
     
  48. philosopherjeff

    philosopherjeff

    Joined:
    Oct 18, 2015
    Posts:
    3
    THANKS MAN TOTALLY FORGOT ABOUT THAT! now it works!
     
  49. UoUo

    UoUo

    Joined:
    Oct 19, 2015
    Posts:
    8
    Hi, let me thank you first for the amazing tutorial, I've follow the tutorial and everything works perfectly (on PC) but I'm having an issue when I export to (Android), I think I remember hearing that if we used Forces Android will detect them, but I'm trying to move the ball in the android version and it just doesn't move, It's like Input isn't working at all for Android.
     
  50. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Android does use forces and all of the internal workings of unity (pretty much).

    What you're missing is the input.

    The current input is from an attached keyboard. You will need to change the input method from keyboard to touches or something platform specific like acceleration... For these, you can search the documentation.
     
    UoUo likes this.