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

Question freezing rotation adds a random force

Discussion in 'Scripting' started by Camrdg, Aug 15, 2023.

  1. Camrdg

    Camrdg

    Joined:
    Jun 8, 2023
    Posts:
    5
    in my game I have a mechanic called high siding. after you pass a certain angle it doesn't go back until enough time goes by. this function works. my problem is once it freezes it starts moving in the direction of the bottom of the car.
    if ((t.rotation.eulerAngles.z < 60 + surfaceAngle) && (t.rotation.eulerAngles.z > 29 + surfaceAngle) && grounded)
    {
    if (leftSide == 0)
    {
    leftSide = leftSide + 2;
    }
    if (leftSide == 2)
    {
    waitL1 = waitL1 - Time.deltaTime;
    if(t.rotation.eulerAngles.z < 40 + surfaceAngle)
    {
    t.rotation = Quaternion.Euler(t.eulerAngles.x, t.eulerAngles.y, 40);
    }
    }
    if (waitL1 < 0)
    {
    leftSide = leftSide - 1;
    waitL1 = 2;
    }
    }
    here's a video with my problem

    if you need anymore of my code I will be happy to provide.
    my brain hurts, this feature has been more complicated than expected.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Anytime you manipulate a rotation or position of a rigidbody directly, you're gonna have creep problems.

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121