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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

RigidBody rotation contraints dont work at all

Discussion in 'Scripting' started by ComputerBruce, Aug 10, 2015.

  1. ComputerBruce

    ComputerBruce

    Joined:
    Aug 6, 2015
    Posts:
    22
    Hi

    I have a simple scene with a cube balancing on a sphere. The cubes x and z are controlled by user input. Imagine a see saw but3d. Anyway, it all works fine except that the cube spins around on top of the sphere even though the Y rotation axis is constrained and at no point in code do I modify the Y axis.

    Here is my script that is attached to the cube:

    Code (csharp):
    1.  
    2. private float tiltSpeed = 3f;
    3. private float tiltX, tiltZ;
    4. private Vector3 localRotation;
    5. Vector3 tempQt;
    6. private Rigidbody rb;
    7.  
    8. // Use this for initialization
    9. void Start () {
    10. localRotation = transform.rotation.eulerAngles;
    11. rb = GetComponent<Rigidbody> ();
    12. tempQt = transform.rotation.eulerAngles;
    13. }
    14.  
    15. // Update is called once per frame
    16. void Update () {
    17.  
    18. }
    19.  
    20. void FixedUpdate(){
    21. float curSpeed = Time.deltaTime * tiltSpeed;
    22.  
    23. //tiltX = Input.acceleration.z + 5f * 2f;
    24. //tiltZ = Input.acceleration.x * curSpeed;
    25.  
    26. tiltZ = Input.GetAxis ("Vertical") * curSpeed;
    27. tiltX = Input.GetAxis("Horizontal") * curSpeed;
    28.  
    29. localRotation.x += (float)tiltZ;
    30. localRotation.z -= (float)tiltX;
    31.  
    32. tempQt = rb.rotation.eulerAngles + new Vector3(localRotation.x, 0f, localRotation.z);
    33.  
    34. rb.rotation = Quaternion.Euler(rb.rotation.eulerAngles + new Vector3(localRotation.x, 0f, localRotation.z));
    35.  
    36. }
    37.  

    Is this a bug or am I doing it wrong?

    Any help getting the Y axis constrained would be greatly appreciated.

    Thanks
     
  2. ComputerBruce

    ComputerBruce

    Joined:
    Aug 6, 2015
    Posts:
    22
    Ok

    I think after some googling I figured it out.

    It seems that you cant rotate an object on 2 axis independent of the third.
    So what I was trying to do is physically impossible and was leading to gimbal lock.

    Is there a way to rotate a plane on x and z while limiting how far it can spin around y?

    What I am trying to achieve is have a cube, that is flattened (like a plane with a little height) tilt left when left key is pressed, right when right key, tilt forward with up key, tilt back with down key.

    All of these work fine on their own. The trouble starts when you press left the up it start to spin around y.

    I want it so that on a phone when you tilt the phone you have to try keep the plane level without the plane spining more than it has to.

    Hope all that made sense :p