Roll a Ball example C# code does not work

Am new to unity and am attempting to follow the tutorial to get acquainted with it, however the example code given in the Roll A Ball game, however the code throws an error.

Error received: Keyword ‘void’ cannot be used in this context

This relates to the line void FixedUpdate ()

Snipped of entire script below:

using UnityEngine;
using System.Collections;

publicclassPlayerController:MonoBehaviour{

publicfloatspeed;
privateRigidbodyrb;

voidStart()
{
rb=GetComponent();
{

voidFixedUpdate()
{
float moveHorizontal = Input.GetAxis (“Horizontal”);
floatmoveVertical=Input.GetAxis(“Vertical”);

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

rb.AddForce(movement*speed);
}
}

This seems to be exactly as shown in the tutorial but does not work. Any help with what I am doing wrong here would be greatly appreciated

Thanks!

Please use the “Insert Code” button to paste your code. Otherwise, the forum messes up the formatting so it’s hard to tell whether you actually had things like “voidStart” in your code.

The Insert button is located right between the movie filmstrip and the floppy disk icon in the editing toolbar.

Unfortunately without code tags I’m not positive how your code looks on your end. The forum software will pretty much butcher anything without them. Simply at a glance though your opening and closing braces are wrong. Additionally you neglected to put spaces in between keywords. Both of those problems are confusing the compiler.

Once I fixed the braces and put spaces in appropriate places the errors went away.

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

Hi Joe and Ryiah, thanks so much for your replies.

I double checked and it was indeed a bracket issue (had 2 opening brackets instead of an opening and a closing…facepalm). Also thanks for showing me how to insert code snippets on the forum! Currently the following code does not throw errors:

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

however I am still having problems getting the ball to move. Everything seems to be set up correctly both with the player object and the input manager settings (please see screen shots attached). Am I missing a setting somewhere? Please let me know, thanks!


When pressing keyboard keys nothing happens

Are you sure “Is Kinematic” should be checked for that ball? Based on the code, I would expect you to need an ordinary (non-kinematic) Rigidbody here.

1 Like

If a rigidbody has “Is Kinematic” set to true then it will not respond to forces like the script is trying to use to move the ball.

http://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html

That was it! Thanks again for the help!

1 Like