Search Unity

Roll-A-Ball -> PlayerController not working

Discussion in 'Getting Started' started by TrueShape, Apr 7, 2015.

  1. TrueShape

    TrueShape

    Joined:
    Apr 7, 2015
    Posts:
    7
    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
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    You need to use GetComponent to access the rigidbody.

    Code (csharp):
    1. GetComponent<RigidBody>().AddForce(movement * speed * Time.deltaTime);
     
    TrueShape likes this.
  3. TrueShape

    TrueShape

    Joined:
    Apr 7, 2015
    Posts:
    7
    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
     
  4. TrueShape

    TrueShape

    Joined:
    Apr 7, 2015
    Posts:
    7
    New Error

    Error CS0246: The type or namespace name <ridigbody> 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<RigidBody>().AddForce(movement * speed * Time.deltaTime);
    }
    }
    .........................................................................................................................................................
    RigidBody and AddForce are Red

    Thanks again for any assistance.

    Ronnie
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    Sorry about that. I keep putting the wrong capitalization on it. Try <Rigidbody> instead.
     
    TrueShape likes this.
  6. TrueShape

    TrueShape

    Joined:
    Apr 7, 2015
    Posts:
    7
    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
     
  7. Soopyyy

    Soopyyy

    Joined:
    Apr 7, 2015
    Posts:
    3
    I have the same problem, the tutorials are a mess!
     
    TrueShape likes this.
  8. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
  9. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Perhaps try something like this...

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float speed = 250f;
    8.  
    9.     private Rigidbody _rb;
    10.     private int _pickupCount;
    11.  
    12.     private void Start() {
    13.         _rb = GetComponent<Rigidbody>();
    14.         _pickupCount = 0;
    15.     }
    16.  
    17.     private void FixedUpdate() {
    18.         float moveHorizontal = Input.GetAxis("Horizontal");
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    21.         _rb.AddForce(movement * speed * Time.deltaTime);
    22.     }
    23.  
    24.     private void OnTriggerEnter(Collider other) {
    25.         if (other.gameObject.tag == "Pickup") {
    26.             other.gameObject.SetActive(false);
    27.             _pickupCount += 1;
    28.         }
    29.     }
    30. }
     
    Lifox and TrueShape like this.
  10. TrueShape

    TrueShape

    Joined:
    Apr 7, 2015
    Posts:
    7
    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
     
  11. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Before we go looking elsewhere, check the input settings ;
    Menu bar Edit->Project Settings->Input ...
    Mine looks like this :

    http://screencast.com/t/umBxV0bn0

    //-------------------------
     
    Deleted User and TrueShape like this.
  12. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    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,
     
    TrueShape likes this.
  13. TrueShape

    TrueShape

    Joined:
    Apr 7, 2015
    Posts:
    7
    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.
     
  14. TrueShape

    TrueShape

    Joined:
    Apr 7, 2015
    Posts:
    7
    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
     
  15. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    You are most welcome Ronnie.

    Posting helps me learn, so I get a benefit too :)
     
  16. jay-williams

    jay-williams

    Joined:
    Feb 7, 2016
    Posts:
    1
    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<Rigidbody>();
    }

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

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

    rb.AddForce(movement);
    }
    }
     
    RoryDR9 likes this.
  17. RoryDR9

    RoryDR9

    Joined:
    Mar 3, 2016
    Posts:
    5
    I'm having the same problem Jay, let me know if you find a solution. Thanks bro.
     
  18. RoryDR9

    RoryDR9

    Joined:
    Mar 3, 2016
    Posts:
    5
    I tried starting a new project and following it again, no luck.
     
  19. Deleted User

    Deleted User

    Guest

    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?

    Code (CSharp):
    1. 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.

    Code (CSharp):
    1. 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?

    Code (CSharp):
    1. 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.
     
    Last edited by a moderator: Mar 11, 2016
  20. RoryDR9

    RoryDR9

    Joined:
    Mar 3, 2016
    Posts:
    5
    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 :)
     
    geethu15 and Seeru-Somni like this.
  21. Seeru-Somni

    Seeru-Somni

    Joined:
    Jul 2, 2016
    Posts:
    1
    Thanks @RoryDR9 !!!
    You sir, just saved my day!!!

    ....stupid tutorials...(mumble, mumble).
     
  22. whalen_on_fools

    whalen_on_fools

    Joined:
    Aug 19, 2015
    Posts:
    1
    Does anybody have a full copy of the script that's updated to work in Unity 5? I'm still having problems with it.
     
  23. Bobonevive96

    Bobonevive96

    Joined:
    Feb 26, 2017
    Posts:
    6
    sorry but I didn't understand what to do after changing the code. Can anyone explain it to me??
     
  24. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    I replied to your post in the tutorial thread. It should help you out.
     
  25. hvarma

    hvarma

    Joined:
    Nov 28, 2017
    Posts:
    4
     
  26. hvarma

    hvarma

    Joined:
    Nov 28, 2017
    Posts:
    4
  27. Adamantin

    Adamantin

    Joined:
    Dec 4, 2017
    Posts:
    1
    This is a full copy of the script that's updated to work in Unity 5:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public Rigidbody rb;
    8.     public float speed = 250f;
    9.  
    10.     private void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.     void FixedUpdate()
    16.     {
    17.         float moveHorizontal = Input.GetAxis("Horizontal");
    18.         float moveVertical = Input.GetAxis("Vertical");
    19.  
    20.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    21.  
    22.         rb.AddForce(movement*speed*Time.deltaTime);
    23.     }
    24. }
     
  28. Monkey_501

    Monkey_501

    Joined:
    Dec 21, 2017
    Posts:
    1
    the problem is not with the script.. the issue is the although you name the script PlayerController it gets named playercontroller in the project scripts folder. change the scripts name (add caps) and it works
     
  29. JomppupomppuX

    JomppupomppuX

    Joined:
    Nov 18, 2018
    Posts:
    3
    @RoryDR9
    When I try it, Unity reports an error message:Cant add script behaviour CallbackExecutor.The script needs to derive from MonoBehaviour!
    And I have a script:
    public class PlayerController : MonoBehaviour
     
  30. Supermclucky

    Supermclucky

    Joined:
    Jan 24, 2019
    Posts:
    1
    Everyone listen to this guy. Capitalize the letter c and it will work.
     
  31. tacoke

    tacoke

    Joined:
    Apr 24, 2022
    Posts:
    1
    I have done everything as described and also written "PlayerController" (case sensitive), but the control still does not work. Does anyone have any other ideas what the problem could be?
     
  32. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please describe your issue.