Roll-A-Ball -> PlayerController not working

Hi total newbie need help. cannot get roll-A-ball player controller working

code entered is this. Public class line error. and rigidbody line error

error messages in unity:

error CS0619 rigidbody is obsolete & CS1061 does not contain a definition for AddForce

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
public float speed;

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

rigidbody.AddForce(movement * speed * Time.deltaTime);
}
}

Any assistance would be much appreciated as I’m a absolute beginner trying to learn this software and I don’t have much coding experience.

Thankyou

Ronnie

You need to use GetComponent to access the rigidbody.

GetComponent<RigidBody>().AddForce(movement * speed * Time.deltaTime);
1 Like

Thanks Ryiah

I have to run to painting class But I will try and plug in the code tonight and let you know if it works

btw I like your cartoon. I will get a selfie cartoon too because cartoons are awesome.

Ronnie

New Error

Error CS0246: The type or namespace name could not be found. Are you missing a using directive or an assembly reference?

code:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
public float speed;

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

GetComponent().AddForce(movement * speed * Time.deltaTime);
}
}

RigidBody and AddForce are Red

Thanks again for any assistance.

Ronnie

Sorry about that. I keep putting the wrong capitalization on it. Try instead.

1 Like

Thanks Again Ryiah, Your very helpful.

Please forgive me for my lack of knowledge.

So I have no code errors but when I test the game I cannot move the ball. I think I need to set up the input manager so Unity recognizes up,down,left,right keys or W,A,S,D.

also I have the public float speed but in the inspector (script) tab I do not have any speed value to set as the tutorial suggest it should.

I assume I have to enter some more code to assign buttons for directions and set a value for force.

How should I proceed so I can test the movement of the ball?

Again, anyone’s assistance is much appreciated as I’m stuck and really want to succeed.

Ronnie

I have the same problem, the tutorials are a mess!

1 Like

@TrueShape .
Please use code tags when posting code.

1 Like

Perhaps try something like this…

using System;
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
    public float speed = 250f;

    private Rigidbody _rb;
    private int _pickupCount;

    private void Start() {
        _rb = GetComponent<Rigidbody>();
        _pickupCount = 0;
    }

    private void FixedUpdate() {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        _rb.AddForce(movement * speed * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other) {
        if (other.gameObject.tag == "Pickup") {
            other.gameObject.SetActive(false);
            _pickupCount += 1;
        }
    }
}
2 Likes

Thank you Kdub

The code runs without errors but I get the same error when I push buttons.

! The referenced script on this behavior is missing.

tried up,down,left,right and W,A,S,D

do not see any code for keyboard controls and I assume this is the issue. It is late and I have an early class so I don’t want to stay up too late. I will research keyboard button controls to see if I can figure out a solution as soon as I can.

If anyone has any insight into this it’s is always greatly appreciated, I feel I’m so close to getting the ball rolling, Literally!"

Sorry for being a newbie and not using code tags. I will always use code tags now thanks to Kdub.

Ronnie

Before we go looking elsewhere, check the input settings ;
Menu bar Edit->Project Settings->Input …
Mine looks like this :

http://screencast.com/t/umBxV0bn0

//-------------------------

2 Likes

Can you confirm when/where this message displays.

This is the first time I’ve seen that message … but I am a relative beginner with Unity.

Regards,

1 Like

OK,

It appears both my Horizontal and Vertical input were set to X axis. I changed The Vertical input to Y axis.

This issue is strange. If I hit play touch any button the error is displayed twice. However if I clear the error while in play mode and start using the directional arrows the camera moves around in scene view but nothing happens in game view.

I’m very confused by this program the moment.

Yay!

I forgot to add a ridigbody component to the ball.

The tutorial now works. Thankyou to Kdub and Ryiah for all the help
You guys rock.

Ronnie

You are most welcome Ronnie.

Posting helps me learn, so I get a benefit too :slight_smile:

im having problems aswell… its not telling me there is a error but still wont work?
using UnityEngine;
using System.Collections;

public class playercontroller : MonoBehaviour
{

private Rigidbody rb;

void start()
{
rb = GetComponent();
}

void fixedupdate()
{
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);

Vector3 movement = new Vector3(moveHorizontal, 0.00f, moveVertical);

rb.AddForce(movement);
}
}

1 Like

I’m having the same problem Jay, let me know if you find a solution. Thanks bro.

I tried starting a new project and following it again, no luck.

I did it recently and it all worked as expected, read the errors. They should tell you what line is wrong. Syntax is everything in programming. Not only does case matter, the capitalization can mean the difference between describing the template for something and one instance of that thing.

If you want to make games, you need to start thinking like a programmer.

The most important thing Roll A Ball taught me was what CTRL-’ does in MonoDevelop.

Things aren’t moving. Why? What line actually moves something? In this case, AddForce.

It’s not working, why? Okay highlight AddForce. CTRL-'. API, what does Rigidbody.AddForce want? Is there an example? Try the example. Does it move now?

rb.AddForce(transform.forward * thrust);

Well, it wants a Vector3, so that must be what transform.forward is. Right away, knowing what transform.forward is seems pretty important, so go right to it’s API page.

The blue axis of the transform in world space.

Okay okay, it’s a Vector3 - otherwise AddForce wouldn’t work at all; but what exactly does it mean?

Debug.Log(transform.forward)

Ok, that works. Now back to my own Vector3. If I set it manually does the object move?

Work backwards and find where the chain is broken.

Found a guy who clarified it for me and made it easier to understand… Got the answer, here it is with the original code just slightly edited to comply with the updates.

Sceysch1 year ago

Unity 5: This will not work in this current form. You cannot directly apply force to the rigidbody attached to an object if the script is also attached to the same object. Going forwards, you will need to assign the rigidbody as a property, and apply the force to the property. In order to do this, we use a public variable, the same way as speed in this video, to declare the rigid body:

public class PlayerController : MonoBehaviour {

public Rigidbody playerRigidBody;

Next, we change the code in the function FixedUpdate to apply force to the property instead of the rigidbody. The new line of code is:

playerRigidBody.AddForce(movement * speed * Time.deltaTime);

The last thing to do is to return to Unity and find the Player Controller Script in the Inspector, when you have the Player object selected. AFTER you have added the rigidbody as a public variable to the code, a new property will appear called Player Rigid Body. Set this to the Player (Rigidbody) object by clicking the circle next to the field and clicking the relevant item from the list.

Hope this helps!

IT DOES :slight_smile:

2 Likes