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 Clamp Rigidbody.MoveRotation based on stored vector

Discussion in 'Scripting' started by George_C, Jun 22, 2023.

  1. George_C

    George_C

    Joined:
    Nov 10, 2017
    Posts:
    6
    Hello, I've been trying to clamp Rigidbody.MoveRotation based on a vector stored Vector3(_startRot) which gets updated when the right conditions are met. My code works as expected if _startRot is equal to zero but breaks if _startRot equals anything other than zero. I've tried a ton of different methods to achieve this most of which have gotten me this far but never further.

    I'm pretty sure this is a very simple problem but I just can't seem to figure it out so any help would be greatly appreciated.


    Below is the code I am having trouble with.
    Code (CSharp):
    1. float inputX = Input.GetAxis("Horizontal");
    2.         float inputY = Input.GetAxis("Vertical");
    3.  
    4.         float heading = Mathf.Atan2(inputX, inputY);
    5.  
    6.         Quaternion lookRotation = Quaternion.Euler(0f, 0f, heading * Mathf.Rad2Deg);
    7.  
    8.         Arrow.rotation = lookRotation;
    9.  
    10.  
    11.         if (_canJump == true && Input.GetAxis("Horizontal") != 0 || _canJump == true && Input.GetAxis("Vertical") != 0)
    12.         {
    13.             float clampedRot = Mathf.Clamp(Arrow.rotation.z, _startRot.z - angleOffset, _startRot.z + angleOffset);
    14.             clampedRot = (clampedRot * 100);
    15.             _myRB.MoveRotation(Quaternion.Euler(0, 0, clampedRot));
    16.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This is not at all what you think it is. It is NOT an angle.

    Do not touch the .x, .y, .z and .w components of a Quaternion. Here's why:

    All about Euler angles and rotations, by StarManta:

    https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

    Quaternion dumper if you insist on peeking inside the ugly parts:

    https://forum.unity.com/threads/making-a-random-item-hover.1450048/#post-9088465

    NOW... as for your original problem, let's find out:

    What are you actually TRYING to do here?

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)
     
    George_C likes this.
  3. George_C

    George_C

    Joined:
    Nov 10, 2017
    Posts:
    6
    Thanks for the reply. I've been able to solve my problem using the links you provided thank you. Also thanks for the advice on proper forum etiquette, I'll make sure to follow it next time I post.

    As for how I fixed it I swapped over to doing this

    Code (CSharp):
    1. float angle = Vector3.Angle(Arrow.up, hitColl.transform.up);
    2.             if (angle > -angleOffset && angle < angleOffset)
    3.             {
    4.                 _myRB.MoveRotation(lookRotation);
    5.             }
    Instead of trying to access the object's z rotation directly, I'm using vector3.Angle to compare the transform-up direction of my arrow to the transform-up direction of my hitCol(used to use _startRot) and store the degrees of rotation between them in a float. I then compare my saved float to my angleOffset to test if it's within its bounds.

    This works exactly as I wanted it to. Thank you.
     
    Kurt-Dekker likes this.