Search Unity

Just started Roll-a-ball, already can't get past lesson 3

Discussion in 'Community Learning & Teaching' started by ADos, Mar 10, 2015.

Thread Status:
Not open for further replies.
  1. ADos

    ADos

    Joined:
    Mar 10, 2015
    Posts:
    4
    Hey everyone, pretty bummed that I'm 30 minutes into my first Unity tutorial and it's apparently teaching me the wrong thing.

    I am on the Moving the player lesson where we have to add force to the rigid body of the sphere, but the code that the lesson teaches doesn't work for me.

    So far I have this, which is exactly what the lesson says:

    usingUnityEngine;
    usingSystem.Collections;

    publicclassplayerController : MonoBehaviour {

    voidfixedUpdate() {


    floatmoveHorizontal = Input.GetAxis ("Horizontal");
    floatmoveVertical = Input.GetAxis ("Vertical");


    Vector3movement = newVector3 (moveHorizontal, 0.0f, moveVertical);

    }

    }

    After the movement variable is made, the lesson says to create rigidbody.addForce, but when I type it in there is no option for addForce. I'm guessing something has been updated since the video was made, but I cannot figure out how to proceed now. Any help is greatly appreciated, I'm really excited to keep working with Unity!
     
    Pokem-Up likes this.
  2. ADos

    ADos

    Joined:
    Mar 10, 2015
    Posts:
    4
    Also worth mentioning, the code in the documentation for RigidBody.addForce() is different than in the video.
     
    Pokem-Up likes this.
  3. Pokem-Up

    Pokem-Up

    Joined:
    Mar 10, 2015
    Posts:
    3
    I am having the same issue.


    In the video "Moving the player - 02", after he saves his script he goes into playmode to move his ball.

    But when I try to do the same, I get the message "All compiler errors have to be fixed before you can enter playmode!"

    Also I get the following message in the console : "No MonoBehavior scripts in the file, or their names do not match the file name."

    In Unity Answer, someone asked a similar question and was told to change part of his script. I followed his instructions, and I still get the same errors.

    Here is the post : http://answers.unity3d.com/questions/919972/roll-a-ball-script-refuses-to-accept-monobehaviour.html

    And here is the script I am using

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyClass : MonoBehaviour
    5. {
    6.     void FixedUpdate ()
    7.     {
    8.         float moveHorizontal = Input.GetAxis ("Horizontal");
    9.         float moveVertical = Input.GetAxis ("Vertical");
    10.  
    11.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    12.  
    13.         Rigidbody.AddForce (movement);
    14.     }
    15. }
    16.  
    Please help !
     
  4. Socrates

    Socrates

    Joined:
    Mar 29, 2011
    Posts:
    787
    First, remember that this tutorial was created for a previous version of Unity. If you are using Unity 5, which it sounds like you are, there were some changes.

    Second, please for the love of sanity, use CODE tags.

    Finally, the "rigidbody" in Unity 4 was a shortcut for GetComponent<Rigidbody>(). If you look at the Unity Documentation here, you will see this in use.

    For a simple program like the tutorial, you can even ignore setting the rigidbody ahead of time, though I would not recommend it for a real program. This results in your code reading:

    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, moveVertical);
    7.  
    8.             GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
    9.         }
     
    Last edited: Mar 11, 2015
    theANMATOR2b likes this.
  5. Socrates

    Socrates

    Joined:
    Mar 29, 2011
    Posts:
    787
    The most likely answer to this is that your MonoBehavior class does not match the name of the file. The two MUST match.

    In your code above, you called it "MyClass", but the tutorial calls the class and file "PlayerController".
     
  6. ADos

    ADos

    Joined:
    Mar 10, 2015
    Posts:
    4
    Well, my code is exactly as you said, with the correct MonoBehavior class and all.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.  
    7.     void fixedUpdate() {
    8.  
    9.         float moveHorizontal = Input.GetAxis ("Horizontal");
    10.         float moveVertical = Input.GetAxis ("Vertical");
    11.  
    12.         var speed = 1;
    13.  
    14.         Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
    15.        
    16.         GetComponent<Rigidbody> ().AddForce (movement * speed * Time.deltaTime);
    17.     }
    18.  
    19.  
    20. }
    No compile errors, but it's still not moving when I press the arrow keys...
     
  7. ADos

    ADos

    Joined:
    Mar 10, 2015
    Posts:
    4
    I swear I tried this exact code before, but now it works. Hopefully people with this problem find this thread later.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : 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.         GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
    16.     }
    17. }
     
    Pokem-Up likes this.
  8. Pokem-Up

    Pokem-Up

    Joined:
    Mar 10, 2015
    Posts:
    3
    I updated my code slightly, following your advices. Now the ball roll slowly along the z axis,
    but I still have no control over its movement.
     
    Last edited: Mar 11, 2015
  9. Socrates

    Socrates

    Joined:
    Mar 29, 2011
    Posts:
    787
    If the ball is moving too slowly, increase the value of the "speed" in the inspector. You can do this while in Play mode, then note what a good value is and put that back into the inspector once you leave Play mode and the changes reset.

    If you cannot turn the ball after getting the speed correct, make sure that you are putting the horizontal value into the movement Vector3. As a possible issue: If you accidentally used "Vertical" for both GetAxis() calls, you would not be looking at the input from the A and D keys.
     
    theANMATOR2b and Pokem-Up like this.
  10. Pokem-Up

    Pokem-Up

    Joined:
    Mar 10, 2015
    Posts:
    3
    Its working now ! I changed the speed to 100, and now I can move the ball.
    I had control of it before, but it was moving so slowly I though it was just rolling on its own until it fell down the edge of the plane...

    Thanks socrates !

    Cant wait to continue on with the tutorial !
     
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please see the official thread at the top of this thread.
     
  12. W1N5T0Nz

    W1N5T0Nz

    Joined:
    Nov 7, 2015
    Posts:
    3
    I am tried all iterations of the code in this thread and none of them worked, I keep getting the message about missing MonoBehavior scripts. Please send help.
     
  13. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    @W1N5T0Nz - Two things:

    1) This is a very old thread. Please don't ressurect an old thread. Start one of your own, please.

    2) Please use the official Q&A threads when asking for support on an official Unity lesson or project. Please ask a new, full and complete question of your own with full details in the official roll-a-ball thread in the "stickies" at the top of this section! Thanks!

    As such, I am closing this thread.
     
Thread Status:
Not open for further replies.