Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity 5: Now the ball from the first tutorial rolls itself

Discussion in 'Physics' started by Robbson, Mar 7, 2015.

  1. Robbson

    Robbson

    Joined:
    Mar 3, 2015
    Posts:
    5
    After a longer absence from touching Unity I just tried the good old Roll-a-ball tutorial project as a small refresh in Unity 5. Unfortunately, as soon as the function AddForce() on the rigidbody component is used (even with no data from the input) the ball starts to roll away in +Z direction after about 8 seconds when I start game mode.

    It's just a script with a the usual MonoBehavior class and FixedUpdate:
    Code (CSharp):
    1.  
    2. void FixedUpdate() {
    3.        Vector3 movement = new Vector3(0.0f, 0.0f, 0.0f);
    4.        GetComponent<Rigidbody>().AddForce(movement);
    5. }
    6.  
    I also tried the tutorial in Unity 4 and there the ball sticks correctly at its original position.

    Any idea?

    Thanks,
    Robbson
     
    gerdw likes this.
  2. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    This might be related to the physx upgrade.

    You're constantly doing something with the rigidbody so it is never set to sleep mode, after a while floating point errors get large enough to move the ball. I've had a similar problem, it can be solved by only calling addforce when you actually have a force to add.
     
  3. Robbson

    Robbson

    Joined:
    Mar 3, 2015
    Posts:
    5
    Yes, probably it's a floating point issue. But if you use debug output of all the related values you still see plain 0.0 values. I would expect rather some very small values like in the transform component.

    It seems to be working when I add if(movement != Vector3.zero) before AddForce().
    Maybe it's catching invalid values or something. :confused:

    But still 0.0 should be an reliable value, especially when nothing is added.
    If there are some problems to be expected it should be mentioned in the tutorial.
     
  4. gerdw

    gerdw

    Joined:
    Mar 13, 2015
    Posts:
    8
    I have the same problem, but can't get it to work. Could you share the script that is working for version 5? Thanks!
     
  5. Robbson

    Robbson

    Joined:
    Mar 3, 2015
    Posts:
    5
    @gerdw
    As I wrote, you just insert the addtional if-statement:

    Code (CSharp):
    1.  
    2. if( movement != Vector3.zero) {
    3.     GetComponent<Rigidbody>().AddForce(movement);
    4. }
    5.  
     
  6. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    You should not call GetComponent<Rigidbody>()every frame though!
     
  7. Robbson

    Robbson

    Joined:
    Mar 3, 2015
    Posts:
    5
    Of course not... but for the sake of a basic tutorial it's ok.
     
  8. gerdw

    gerdw

    Joined:
    Mar 13, 2015
    Posts:
    8
    I still keep getting message to fix errors. This is the script:


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

    if( movement != Vector3.zero) {
    GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
    }
    {
     
  9. kippert

    kippert

    Joined:
    Mar 13, 2015
    Posts:
    3
    You have some mangled braces in your script gerdw. You should have something like (indents really help to know what you are missing :) ):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.   public float speed;
    7.  
    8.   void FixedUpdate()
    9.   {
    10.     float moveHorizontal = Input.GetAxis ("Horizontal");
    11.     float moveVertical = Input.GetAxis ("Vertical");
    12.    
    13.     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    14.    
    15.     if( movement != Vector3.zero) {
    16.       GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
    17.     }
    18.   }
    19. }
     
  10. gerdw

    gerdw

    Joined:
    Mar 13, 2015
    Posts:
    8
    thanks for the tip. This is what I have right now, but the ball is still moving up and down, while it should lie still on the ground...

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

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    Do you have a controller connected? It could be stuck giving 0.05 vertical input for example.
     
  12. gerdw

    gerdw

    Joined:
    Mar 13, 2015
    Posts:
    8
    No, there is no controller connected. I am working on a MacBook without any external controllers.
     
  13. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    You might want to submit a bug report then.