- Home /
Ball won't move in the Roll-a-Ball tutorial
Hey Guys. I am new to Unity, so apologies if this is very Simple. I have followed the tutorial up to Part 2 of 8, and the ball will not move at all for some reason. I have restarted unity, and deleted the game and made a new one, which, also does not work, and has the same issue. I am perplexed as I have spent a week trying to troubleshoot, but to no avail. My code is verbatim of the tutorials, as it was copy/pasted from the box below. Gravity does work by lifting the player, then beginning the game, which results in it dropping back to rest upon the plane. When I inserted the script into VisualBasic, no errors appeared. However, upon re-opening VisualBasic, a line endings error appeared, which I then pressed the fix button. 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);
}
}
Thank you!
Did you set a speed value in the inspector? speed defaults to 0 so "movement * speed(0) = no movement. You can also add Debug.Log("foo")
somewhere to check if the code is actually running. The word "foo" would show in the console if it is.
Hey @Pengocat,how would I go about this? something like speed = 1? Thank you!
Answer by oStaiko · Feb 05, 2017 at 08:01 AM
Your code is perfect, however Unity has another very important aspect to it, the Inspector. The inspector is something you wont be used to if you have a background in any other simple form of programming, so it's common for new users to get a bit confused. Basically, the inspector allows you to modify public variables in Unity, and even view and change them while the game is running! What you want to do here is go in to your new scene with the ball, click on the ball that has the script attached to it, find the script in the inspector tab, and look for where it says "Speed". You'll notice a box with a 0 in it to the right, and you'll want to change this to any higher number, let's go with 1 for now. If that doesn't fix your issues, post a screenshot of your inspector tab so we can help more.