Search Unity

Official Roll-a-ball Tutorial Q&A

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

  1. gizmo1990

    gizmo1990

    Joined:
    Jul 12, 2012
    Posts:
    32
    Thanks for the reply Adam. I'm pretty sure I set the mode to 3D originally but I restarted and definitely set it to 3D. Unfortunately I get the same result, the camera is set to Orthographic and there is no skybox?? I can't think what else I'm missing?
    Could you tell me how I set the skybox manually? Could this have something to do with it? Also, how do I get that nice rim light shader which the sphere and other gameobjects have??

    *Edit* Found the 2D and 3D mode settings document and followed the settings for 3D mode setup in the Lighting window. All sorted now. :) Reckon there's a little bug with the initial 2D and 3D setup tho.
     
    Last edited: Feb 4, 2016
  2. 666forhope

    666forhope

    Joined:
    Feb 8, 2016
    Posts:
    1
    @Adam Buckner Hello i have a problem, when i do all steps the one eror left. Its nothing in the script but it failed load the package at all. I tried to reimport all but it doesnt work. Can u help me?
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    If you can consistently reproduce this issue, can you please submit a bug report and get me the number, as I cannot reproduce this.
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please post the details of your problem?

    We need enough information to be able to know what your issue is, exactly, to be able to solve it.

    Please write a complete and accurate description of your problem and include any relevant supporting material, such as screen captures and code snippets.

    Please display any images in-line so we can see them easily and post any code snippets using code tags. (How to use code tags: http://forum.unity3d.com/threads/using-code-tags-properly.143875/)
     
  5. lppv

    lppv

    Joined:
    Feb 9, 2016
    Posts:
    1
    im having an issue when i start the game my ball rolls but i cant move it using my keyboard
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class controller : MonoBehaviour {
    5.  
    6.     private Rigidbody rb;
    7.  
    8.     void Start ()
    9.     {
    10.         rb = GetComponent<Rigidbody> ();
    11.     }
    12.     void fixedupdate()
    13.     {
    14.         float movehorizontal = Input.GetAxis ("Horizontal");
    15.         float movevertical = Input.GetAxis ("vertical");
    16.  
    17.         Vector3 movement = new Vector3 (movehorizontal, 0.0f, movevertical);
    18.  
    19.         rb.AddForce (movement * 2);
    20.     }
    21.  
    22. }
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Don’t forget C# is CaSe SeNsItIvE! So:
    • start is not Start
    • update is not Update
    • fixedupdate is not FixedUpdate
    You must make sure you not only use the correct spelling but the correct Casing or Capitalization.

    In your case both FixedUpdate and Vertical are not written correctly.

    By convention, your class "controller" should be "Controller", and movehorizontal and movevertical should be moveHorizontal and moveVertical, but these won't prevent the script from working.
     
  7. jsbryan1

    jsbryan1

    Joined:
    Feb 16, 2016
    Posts:
    4
    So how would you change the camera and the movement so that the camera always follows the forward direction of the ball, and the ball always moves forward using the W or up key? I want to be able to add a maze to my roll-a-ball game, but it's not a fun maze if you can see from above. It's be better if the camera simply followed closely behind the ball, changing which direction it faces according to the direction you send the ball.
     
  8. Redek

    Redek

    Joined:
    Feb 16, 2016
    Posts:
    1
    Hey,
    So far I've got this:

    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

    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);
    }
    }

    The ball doesn't move :(
    Can you help?
     
  9. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Have you had a look through the Standard Assets pack from Unity on the Asset Store?
    theres a Rollerball Controller in there that may what your after to study, think theres an example scene in there too so you can see it in action to see how it works under the hood so to speak.
     
    Last edited: Feb 16, 2016
  10. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Look out for case sensivity, the Start and FixedUpdate functions need to be Capitalised correctly for them to work as its case sensitive.

    change where you currently have
    Code (CSharp):
    1. void start ()
    2. void fixedupdate ()
    to
    Code (CSharp):
    1. void Start ()
    2. void FixedUpdate ()
    The same case sensitive problems should be addressed with the Axis, as the V and H for your axis should also be Upper case.
    Code (CSharp):
    1.  
    2. float moveHorizontal = Input.GetAxis ("Horizontal");
    3. float moveVertical = Input.GetAxis ("Vertical");
     
    Last edited: Feb 16, 2016
  11. jsbryan1

    jsbryan1

    Joined:
    Feb 16, 2016
    Posts:
    4
    I've used it. I've also tried all the camera systems that come with the standard assets. None of them work primarily because the player character spins as it moves.
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You could try to orient the camera to face the direction of the x-z values of the rigidbody velocity... Just a stab In the dark. I've not tried it.
     
  13. Suffragette

    Suffragette

    Joined:
    Feb 19, 2016
    Posts:
    3
    Hey! I got that so far by using unity 5,


    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

    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);
    }
    }

    there a appears no error but the ball is not moving... can anyone help??
     
  14. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    When posting code on the forums, you must use code tags.

    Please learn to use code tags properly: http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Have you followed all of the steps in the video and the upgrade guide?

    Is the script attached to the Player GameObject?
    Did you save your script?
    Do you have a Rigidbody attached to the Player GameObject?
    Are you using the ASDW or Arrow Keys? Or have you remapped your Input in the Input Manager?
     
  16. Suffragette

    Suffragette

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

    Sorry! now it s right, i hope
    Yes I followed all steps
    I attached the script to the Player GameObject
    I saved the script
    I have attached a Rigidbody the Player GameObject
    What is ASDW or Arrow Keys? And which Input i have to remap to the Input Manager?
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The ASDW keys or the Arrow keys are the keys you use to move the ball unless you have remapped them in the Input Manager. By default the axes "Horizontal" and "Vertical" are mapped to A/D and W/S, and the left/right and up/down arrow keys. How were you trying to move your ball?
     
  18. Laine2000

    Laine2000

    Joined:
    Feb 23, 2016
    Posts:
    1
    No matter what I do, the ball won't move. I checked the code like 20 times and it is word for word like the tutorial. Nothing seems to work, please help me!!!!!!!!!!!!!!!
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This is hard to say without more detailed information. Essentially it's like going into the garage and saying "My car is broken. What do I do to fix it?" There could be any number of problems and therefor any number of solutions.

    Without more detailed information, I'd suggest going thru the last lesson again and checking your step in both the code (remembering that both spelling and capitalisation are important, as C# is case sensitive), and the steps done in the editor and make sure that everything is correct.

    One tip is to do a "Save As" at the end of each lesson so you can go back to the last working version of the project and follow it thru the steps again, without having to back-track using a partly completed project.

    Specifically, in your case, I'd double check the spelling and capitalisation. The most common mistakes are misspellings or wrong capitalisation of the axes Horizontal and Vertical, or the functions Start and FixedUpdate. The other common issues are to have failed to save the script, to have not assigned the script to the GameObject or to have a speed value of 0.
     
  20. Sturm7ruppen

    Sturm7ruppen

    Joined:
    Feb 25, 2016
    Posts:
    2
    Hello, I must be missing something here in this last step. My blank "Win text" won't change to the "You got them all!" text when the count number comes to the desired number (5 in my case).
    Here it is:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ControleJogador : MonoBehaviour
    6. {
    7.  
    8.     public float speed;
    9.     public Text countText;
    10.     public Text winText;
    11.  
    12.     private Rigidbody rb;
    13.     private int count;
    14.  
    15.     void Start ()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.         count = 0;
    19.         SetCountText();
    20.         winText.text = "";
    21.     }
    22.  
    23.     void FixedUpdate ()
    24.     {
    25.         float moveHorizontal = Input.GetAxis("Horizontal");
    26.         float moveVertical = Input.GetAxis("Vertical");
    27.  
    28.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    29.  
    30.         rb.AddForce(movement * speed);
    31.     }
    32.  
    33.     void OnTriggerEnter(Collider other)
    34.     {
    35.         if (other.gameObject.CompareTag("Pickup"))
    36.         {
    37.             other.gameObject.SetActive(false);
    38.             count = count + 1;
    39.             countText.text = "Count: " + count.ToString();
    40.         }
    41.     }
    42.  
    43.     void SetCountText ()
    44.     {
    45.         countText.text = "Count: " + count.ToString();
    46.         if (count >= 5)
    47.         {
    48.         winText.text = "You got them all!";
    49.         }    
    50.     }
    51. }
     
  21. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Is your score going up? Do you have 5 or more objects?
     
  22. Sturm7ruppen

    Sturm7ruppen

    Joined:
    Feb 25, 2016
    Posts:
    2
    I have 16 rotating pickup-able squares. The count number goes up normally.
    My Win Text does begin blank but the 5 count marker won't make it appear. The text is correctly linked to the "Player>ControleJogador (Script)>Win Text" just like the Count Text is to the "Player>ControleJogador (Script)>Count Text". No clue what's happening here.
     
    Last edited: Feb 25, 2016
  23. shrooq

    shrooq

    Joined:
    Jan 12, 2016
    Posts:
    2
    How would I use a first person camera for the rolling ball?
     
  24. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    In start, can you assign a value to the win text? How does it display on the screen? If you put a string value into the text components text field, how does it display?
     
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm not sure I understand the question.
     
  26. shrooq

    shrooq

    Joined:
    Jan 12, 2016
    Posts:
    2
    I am making a maze and I am trying to make my ball from a first person perspective. But when I do the camera rotates with the ball. How could I fix that?
     
  27. DiamondEmperor

    DiamondEmperor

    Joined:
    Feb 26, 2016
    Posts:
    1
    I got the error
    Assets/Scripts/PlayerController.cs(25,27): error CS1547: Keyword `void' cannot be used in this context
    It is at the void for onTriggerEnter I do not know how to fix it here 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);

    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("Pick Up"))
    {
    other.gameObject.SetActive(false);
    }
    }
    }
     
  28. phgamedev

    phgamedev

    Joined:
    Jan 29, 2016
    Posts:
    8
    Hi, not sure if this is the right place to ask... I successfully finished the tutorial (it was really great!!) and now I'm trying different things to modify it.
    I want to:
    1. Replace the ball with a rectangular mesh object I made in Blender
    2. Make the mesh object move like a boat. (with one end being the front)
    I've had success making a prefab cube (elongated into a rectangle) move the way I want, but am having a really hard time figuring out how to make the mesh object move properly.
    I have tried:
    • adding a Rigidbody
    • adding the PlayerController script (from the Roll-a-Ball tutorial) to the mesh object
    • adding a boat movement script from an Aaron Hibbard tutorial (because I want the mesh object to move like a boat)
    • adding a Capsule Collider
    When I press play, my mesh object sort of points forward and slowly sinks through the ground. If I change the Y axis position to 1 (to make sure it's above the plane) it flies towards the camera instead.

    If anyone has any ideas on what I'm doing wrong, I'd really appreciate some help!! I posted the code for both my scripts here, if it helps.

    Thank you!!
     
  29. juandpalacio

    juandpalacio

    Joined:
    Feb 26, 2016
    Posts:
    1
    I'm having the same problem.. How did you figure it out?
     
  30. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Have you not watched the tutorial? We describe this issue in detail. Try watching the camera session.
     
  31. Suffragette

    Suffragette

    Joined:
    Feb 19, 2016
    Posts:
    3
    thanks a lot!!! it works!
     
  32. KusaPi

    KusaPi

    Joined:
    Dec 6, 2015
    Posts:
    3
    My game lag when i press play , and the system.nullReferenceExpectation appeared in monodevelop . how shouldI fix it ?
    Please someone help me~~~~~~~~~~ ToT
     

    Attached Files:

  33. KusaPi

    KusaPi

    Joined:
    Dec 6, 2015
    Posts:
    3
    Btw the count text does not show the number
     
  34. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon,

    Just wondering have you dragged the TextBox for your score from the scene into the inspector slot for your playercontroller?
     
  35. Yaw-Loon

    Yaw-Loon

    Joined:
    Mar 2, 2016
    Posts:
    2
    I found the manual wasn't updated.When I use this script ,
    the ball won't move and get an error:Object reference not set to an instance of an object

    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.     void Start ()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     void FixedUpdate ()
    17.     {
    18.         float moveHorizontal = Input.GetAxis ("Horizontal");
    19.         float moveVertical = Input.GetAxis ("Vertical");
    20.  
    21.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    22.  
    23.         rb.AddForce (movement * speed);
    24.     }
    25. }
    26.  
    And the official manual said the standard example are like this

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ExampleClass : MonoBehaviour {
    6.     public float thrust;
    7.     public Rigidbody rb;
    8.     void Start() {
    9.         rb = GetComponent<Rigidbody>();
    10.     }
    11.     void FixedUpdate() {
    12.         rb.AddForce(transform.forward * thrust);
    13.     }
    14. }
    15.  
    16.  
    I fall in a puzzle,until I found this

    [missing image]

    Then I run it succeful.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. using System.Collections;
    5.  
    6.  
    7. public class PlayerController : MonoBehaviour {
    8.  
    9.  
    10.         public float speed;
    11.  
    12.  
    13.     void FixedUpdate () {
    14.                 float moveHorizontal = Input.GetAxis("Horizontal");
    15.                 float moveVertical = Input.GetAxis("Vertical");
    16.  
    17.                 Vector3 direction = new Vector3(moveHorizontal, 0.0f, moveVertical);
    18.  
    19.                 this.GetComponent<Rigidbody>().AddForce(direction * speed);
    20.         }
    21.  
    22. }
    23.  
    _:)зゝ∠)_
    I wanna a NEW manual...
     
  36. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm afraid you have missed the point with all of the code you have posted.

    A "Missing Reference Exception" means that you are trying to access an object that you don't have a reference to. This is a problem, most likely, with the way you have set up your project. The most common is to have forgotten to add a Rigidbody component to the current GameObject.

    All of the code you have posted is arguably valid in one form or another, and none of them are related to missing references. However, in your "working" example you are "Getting the Component" every frame with this.GetComponent<Rigidbody>().AddForce(direction * speed); in the FixedUpdate function. This is definitely NOT recommended. This is inefficient and can slow down your game. If you do this in more than a few scripts, you could force your game to grind to a halt.

    The original code in the tutorial is valid and should be the code you use.

    If you have having issues, eg: Missing Reference Exceptions, you should solve that problem - not try random new code solutions.

    Try the original code now and see if it works. If the "working" example works on your current setup, then the original code in the project should work as well, and this is the recommended code.
     
    OboShape likes this.
  37. KusaPi

    KusaPi

    Joined:
    Dec 6, 2015
    Posts:
    3
    Oh geez my bad , thanks man =D
     
  38. Flashfyre

    Flashfyre

    Joined:
    Mar 1, 2016
    Posts:
    2
    Hi Adam, I'm having a difficulty which seems pretty unique, having read through the posts on this forum i don't think anyone else is having it. When the ball goes through a rotation, the camera follows it through the rotation by rotating itself. Posting a video so it's clearer.
     
  39. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Flashfyre likes this.
  40. Flashfyre

    Flashfyre

    Joined:
    Mar 1, 2016
    Posts:
    2
  41. Yaw-Loon

    Yaw-Loon

    Joined:
    Mar 2, 2016
    Posts:
    2
    Thank you for help...finally I found my code have a difference to original code...
    The original version is "rb = GetComponent<Rigidbody>();",but my really version is "rb.GetComponent<Rigidbody>();"...
    I will take my code seriously thereafter.
     
  42. DigiG

    DigiG

    Joined:
    Mar 4, 2016
    Posts:
    1
    Have you selected Player to script? Select Player from hierarchy, then in the inspector window select Tag drop down then select Player. Otherwise your code won't be hooked up to the ball (Player) if left as untagged!
     
  43. doodleasa

    doodleasa

    Joined:
    Mar 5, 2016
    Posts:
    1
    on 2 of 8 the (Ctrl + ') command didn't work i also tried (Ctrl + alt + m, Ctrl + H)
    plz help
     
  44. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning,
    can you elaborate a bit more please, sorry, not quite sure why you would need this line.

    The player (ball) rotates as it should by using physics and adding a force in the direction that we want to nudge it like pushing a real ball along the floor, we don't need to set the rotation at all, just let physics do its thing.

    the line you have there, is basically taking a Vector3 with 0 for each value and multiplying it by time.deltatime with is the time different between Updates (frames), but multiplying any number by 0 will always get 0 as the answer.

    But if you could explain a little further as to what you came up against to try this, and we can work towards getting you up and rolling again :)
     
  45. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
  46. RasgorCV

    RasgorCV

    Joined:
    Mar 5, 2016
    Posts:
    2
    Hi, I'm new to Unity development and I've just finished the tutorial on Roll a Ball. My issue is that after I've built the application it shows this screen:



    And not the game screen. Is there a solution for this? I'm currently using a Win 8.1 for development and I followed every step on the tutorial. Playing the game on the Unity IDE works fine but when I build and try to run it this happens :(
     
  47. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Have you ensured that your current game scene is included in the build as Adam describes?
    http://unity3d.com/learn/tutorials/projects/roll-a-ball/building?playlist=17141

    within the File > build Settings, if in doubt remove them by highlighting and pressing delete then adding open scenes.
     
  48. RasgorCV

    RasgorCV

    Joined:
    Mar 5, 2016
    Posts:
    2
    I did as you suggested and it worked :D Thank you!

    Though I had previously followed the video's suggestion on dragging and dropping the scene on the build settings, for some reason it caused the issue I described, I'm not sure why this could have happened.
     
  49. phamtq

    phamtq

    Joined:
    Mar 8, 2016
    Posts:
    6
    I'm just getting started in Unity 5 with very little programming experience and was wondering if someone could clarify something for me. In Lesson 2:1 Moving the Camera, I was wondering if this:

    Code (CSharp):
    1. void Start () {
    2.         offset = transform.position - player.transform.position;
    3.     }
    was the same as this:

    Code (CSharp):
    1. public GameObject camera;
    2.  
    3. void Start () {
    4.         offset = camera.transform.position - player.transform.position;
    5.     }
    Also, if it's not included, is it because of redundancy? Because we're already in the camera game object and editing its script?
     
  50. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    yep, the transform component is avaiable to reference directly from the script if it is attached to the gameobject itself, in this case its attached to the camera, so you are referencing its transform.

    using your second case, this could be used if you are wanting to change another gameobject in your scene that you can drag into the inspector into your public GameObject slot, and then reference it, and then its transform.

    Nice one for doing a bit of digging to find that out, keep it up ;)