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!

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.

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 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.

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!";
	}
}

}

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