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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Cross-Post: Rigidbody Freeze Rotation Ignored During Non-Cubic Collisions?

Discussion in 'Scripting' started by RyogaH, May 10, 2020.

  1. RyogaH

    RyogaH

    Joined:
    Mar 28, 2020
    Posts:
    14
    After posting to the Physics forum, I've realized this may be just as much of a Scripting question. Since I can't move a post from one forum to another, I'm left with no alternative except to cross-post.

    TL;DR: I need all play objects to stay at Z=0 with their tops pointing perpendicular to the X-Y plane such that the orthographic camera above them [at (0, 0, -100)] always sees their tops, all the time, while still being able to bounce off each other, imparting single-axis spin on their local Y-axes. Even with frozen Rigidbody rotations and position, the play objects are violating these constraints upon collision between cubic and non-cubic colliders.

    Much more detail -- the issue leading to this post -- is at https://forum.unity.com/threads/rig...n-ignored-during-non-cubic-collisions.886513/.

    Thank you in advance for any helpful thoughts!
     
  2. RyogaH

    RyogaH

    Joined:
    Mar 28, 2020
    Posts:
    14
    While I'm hoping someone smarter than me can come up with an elegant solution to this problem, here is the scripting workaround I'm settling on, for now:

    Code (CSharp):
    1. void Update() {
    2.     // Other code which handles player input
    3.     CheckOffAxisRotation()
    4. }
    5.  
    6. void CheckOffAxisRotation() {
    7.     // Pin to Z=0
    8.     transform.position = new Vector3(
    9.         transform.position.x, transform.position.y, 0
    10.     );
    11.  
    12.     // Keep top pointing "up" (toward the orthographic camera)
    13.     transform.LookAt(
    14.         transform.position + transform.forward, Vector3.back
    15.     );
    16. }
    This appears to be resolving the side-effects of the unwanted rotations pursuant to collisions between cubic and non-cubic Colliders, but it does not solve the mysterious rotations in the first place. I'd much rather just not have the objects rotating on any axis other than Y, at all.