Roll-A-Ball tutorial isn't correct/My ball won't move?
Right then. I don't pretend to be code savvy;but when following the tutorial, I noticed my ball simply would not move. I followed up to part 3, where he instructs how to make a simple rollaball game.
using UnityEngine;
using System.Collections;
public class Playermovement : MonoBehaviour
{
void FixedUpdate ();
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
Rigidbody.AddForce(movement);
I need help! It just mysteriously doesn't work no matter how many other code snippets I use!
I seem to be having a similar issue with the Roll A Ball tutorial. After follow all the previous steps easily once having saved my script and checking how the ball moves it remained motionless. Having tried several times to add small changes people suggested to the scrip to see what the issue was I received errors. going back to my original script I had no errors the ball simply did not move. I feel I have missed a step. Attached is my latest script with added Float speed and $$anonymous$$ovement * Speed.
$$anonymous$$aybe you can try considering that your code is completely wrong before saying such things ! This code won't EVEN CO$$anonymous$$PILE
Answer by kromenak · Oct 23, 2014 at 06:26 PM
From your code snippet, it looks like you're using incorrect function syntax (kind of surprised this code even compiles?)
You should do something like this:
void FixedUpdate() // <-- DO NOT PUT A SEMICOLON HERE!
{
// Put your code between the braces.
}
Additionally, you shouldn't use "Rigidbody" the way you're using it. You want to use "rigidbody" (lowercase) to access the instance of the rigidbody on the object, as opposed to the Rigidbody class itself.
You also need to make sure the object you're attaching this script to has a "Rigidbody" component attached to it.
If this doesn't make sense, I'd suggest checking out some Unity and C# tutorials.
This code won't even compile, so there is no way it 'would not move' In addition to the issue pointed out by @kromenak, line 14 is wrong. It should be:
rigidbody.AddForce(movement);
...with a lower case 'r'. And the rest of the script is missing, so we cannot verify the code. You need to go back and compare character by character your script against their script. Also, movement may not be large enough...especially if your surfaces have friction. If so, try this:
rigidbody.AddForce(movement * 10.0f);
Co$$anonymous$$g up with so many problems. It demands that 'rigidbody' isn't a variable, even though i've seen so many other people use it. Its saying I need to remove the ()'s from around the (movement); part. Now it's saying playermovement already has a variable called 'movement'. Seriously what? This is not user friendly at all.
Just look up to basic tutorials, no need to come to this forum to ask for solutions you can definetly find on your own !
@OpelBlitz - Without seeing your code, it is unclear what is causing issues with 'rigidbody'. Here is a fixup of your code that compiles. Note I've added a 'speed' variable.
using UnityEngine;
using System.Collections;
public class Playermovement : $$anonymous$$onoBehaviour
{
float speed = 3.5f;
void FixedUpdate () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical) * speed;
rigidbody.AddForce(movement);
}
}
This is what I've added according to the 2nd tutorial video:
using UnityEngine; using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour{
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);
rigidbody.AddForce(movement * 10.0f);
}
}
Answer by iBayan · Jun 13, 2016 at 06:06 AM
Hello, I had the same problem Here what you need to do : -FIRST you have to enter this code exactly in the script
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);
}
}
-SECOND you have to click your ball from the Hierachy window and in the Inspector window select add component - physic - RigidBody (THIS IS WHAT CAUSE THE PROBLEM YOU DID NOT ADD A RIGIDBODY TO YOUR BALL THATS WHY IT WONT MOVE)
I did it these you mentioned. But the ball stayed motionless.
Copied the code, my sphere is a rigid body, still won't work.
Thanks ¡¡¡ you´ve made my day ...¡¡¡ I was searching for hours, losing my $$anonymous$$d until I found and followed your advice...works great ¡¡¡¡
Answer by mrh0rrible · Aug 11, 2017 at 06:53 AM
I got this to work by setting the initialization to
public float speed = 0.0f;
I think you need to be explicit in the declaration for it to work.
This is what got it working for me when it wouldn't show the public variable in the Inspector view. Thanks!
Answer by DobroAlex · Oct 22, 2019 at 06:16 PM
Okay, ultimate code solution: There seems to be the problem with speed -- it's declared but never initialized. My solution is: using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed = 10.0f;
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);
}
}
Or you can access speed as: rb.AddForce (movement * this.speed);
OR you may skipped part of the lesson there public varibale is changed like an object property
Answer by Mogwyre · Mar 12, 2017 at 06:42 PM
Had the same issue with this tutorial. I'm new to coding with a rough background in js.
I commented out "public float speed;" and then multiplied the movement by 10 as someone previously suggested in this thread. It works. I think this is an error on the tutorial's side.
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerController : MonoBehaviour {
// public float speed; public Text countText; public Text winText;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * 10.0f);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 12)
{
winText.text = "YOU WON, IDIOT!";
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613165804im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Roll a Ball tutorial - Pick ups stop rotating when i add more than one 1 Answer
roll the ball - player not moving. Script seems fine,Roll a ball not rolling, should I add speed ? 1 Answer
Why am i getting a Null Referencce Expectation in roll a ball? 1 Answer
camera rotate and follow the ball 0 Answers