Search Unity

Sphere rolls backwards and forwards at the same time?

Discussion in 'Physics' started by ReyLuna, Dec 9, 2019.

  1. ReyLuna

    ReyLuna

    Joined:
    Nov 26, 2019
    Posts:
    3
    Hello,
    I'm new to programming and Unity and have recently started my first project.

    The player controlls a sphere that is being pushed forward by a forward force. The sphere is rolling along a very long cube which serves as a track.

    In the beginning it works, but at a certain point the sphere appears to go forward and backward at the same time. The rotation of the sphere indicates forward movement but in relation to the track it seems to go backwards at the same time.

    I´ve uploaded a video of the problem on YouTube, since it's a bit hard to describe:


    Code that's moving the sphere:
    Code (CSharp):
    1. public class MovePlayer : MonoBehaviour
    2. {
    3.     public Rigidbody rb;
    4.  
    5.     public float forwardForce = 2000f;
    6.     public float sidewaysForce = 500f;
    7.     private bool isJumping = false;
    8.  
    9.  
    10.     void FixedUpdate()
    11.     {
    12.         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
    13.  
    14.         if (Input.GetKey(KeyCode.RightArrow))
    15.         {
    16.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
    17.         }
    18.  
    19.         if (Input.GetKey(KeyCode.LeftArrow))
    20.         {
    21.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
    22.         }
    23.  
    24.         if (Input.GetKeyDown(KeyCode.Space))
    25.         {
    26.             if (isJumping == false)
    27.             {
    28.                 rb.AddForce(new Vector3(0, 4, 0), ForceMode.Impulse);
    29.                 isJumping = true;
    30.             }
    31.         }
    32.     }
    33.  
    34.     private void OnCollisionEnter(Collision collision)
    35.     {
    36.         if (collision.gameObject.tag == "Track")
    37.         {
    38.             isJumping = false;
    39.         }
    40.     }
    41. }
    I'm not even sure if it is a physics problem or if I've just forgotten something, but maybe you can help?

    Thanks a lot!
     
  2. ReyLuna

    ReyLuna

    Joined:
    Nov 26, 2019
    Posts:
    3
    I've just tried replacing the sphere with a cube (and no friction so that the cube won't roll). It's exactly the same problem as with the sphere.
     
  3. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    My guess is that your suffering from an aliasing artifact:

    The texture on the box is repetitive, you’re going on very high speeds and you have a limited framerate. Velocity multiplied by frame delta time is the distance travelled between two frames. If this travelled distance is close to the tiling distance of your ground texture, you get the impression that the floor is not moving, moving slow or moving backwards. This effect appears everywhere when two frequencies meet that are close to each other: in your case the frequency of moved distance and the frequency of the floor texture.

    The same thing happens with the rotating sphere. If there is a full rotation between two frames, you just see the sphere in the same pose as in the frame before.

    there is no direct approach overcome this. Some ideas though:
    - Limit the velocity
    - Use larger texture or increase its scale
    - Put large objects near the track that don’t repeat. A mountain here, a tower there.
    - Curve the track
    - Use Motion Blur

    Here’s a video that explains some of this:
     
  4. ReyLuna

    ReyLuna

    Joined:
    Nov 26, 2019
    Posts:
    3
    Hello,
    Thanks so much for your answer and the detailed explanation!
    I'd never have thought it was an optical illusion of sorts.

    I changed the tiling from 6x600 to 1x100 and the velocity from 500 to 300 and that was enough to fix the problem for now.

    So thanks for saving my first ever project!
     
    tjmaul likes this.
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Just a side note: don't multiply the force by Time.deltaTime. A force is a time-independent magnitude. Once the force is applied, the physics engine applies it to the rigidbody accordingly to the current physics time step. That multiplication will provide different physics behaviors if you modify the fixed timestep later in the project.