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 How to rotate an object based on rotation speed

Discussion in 'Scripting' started by Blackfire-Studio, Sep 22, 2023.

  1. Blackfire-Studio

    Blackfire-Studio

    Joined:
    Dec 17, 2013
    Posts:
    178
    Hi there,
    I'm building a simple character controller and I would like to know how you would approach the following :
    As the character moves I would like to bend/rotate it toward the direction of the rotation with an angle based on the rotation speed.
    i would like to be able to control that bending angle using an animation curve for example to define the angle based on rotation speed factor.

    Here's my character controller.
    Code (CSharp):
    1. private void Update()
    2. {
    3.     Vector2 direction2D = _playerControls.Player.Movement.ReadValue<Vector2>();
    4.     Vector3 direction3D = new Vector3(direction2D.x, 0.0f, direction2D.y);
    5.  
    6.     float magnitude = direction3D.magnitude <= JoystickDeadZone ? 0.0f : direction3D.magnitude;
    7.     // Move
    8.     //Transform.position += direction3D.normalized * magnitude * MaximumSpeed * Time.deltaTime;
    9.     // Rotate
    10.     Quaternion newRotation = Quaternion.LookRotation(lastFrameDirection3D.normalized, Vector3.up);
    11.     Transform.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, RotationSpeed * Time.deltaTime);
    12.  
    13.     if (direction3D.magnitude != 0.0f)
    14.     {
    15.         lastFrameDirection3D = direction3D;
    16.     }
    17. }
    I don't have any idea on how to implement that right now. I first thought to rely on the Inputs but I think that it would be better to rely on the actual object rotation speed.
    Do you have any idea about his to setup such behavior ?

    Thanks a lot !
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,104
    You can use AnimationCurve.Evalute to get the speed based on the joystick input, and then use the formula for calculating distance based on velocity and time to get the final rotation:
    Code (CSharp):
    1. [SerializeField] private AnimationCurve inputToSpeedCurve;
    2.  
    3. private void Update()
    4. {
    5.     Vector2 inputVector = _playerControls.Player.Movement.ReadValue<Vector2>();
    6.     float rotationInput = Mathf.Abs(inputVector.x) - JoystickDeadZone;
    7.     if(rotationInput <= 0f)
    8.     {
    9.         return;
    10.     }
    11.  
    12.     float rotationSpeed = inputToSpeedCurve.Evaluate(rotationInput);
    13.     float time = Time.deltaTime;
    14.  
    15.     // Formula: s = t * v
    16.     // https://en.wikipedia.org/w/index.php?title=Special:MathWikibase&qid=Q3711325
    17.     float rotationDegrees = time * rotationSpeed;
    18.  
    19.     transform.Rotate(Vector3.up, rotationDegrees);
    20. }
     
  3. Blackfire-Studio

    Blackfire-Studio

    Joined:
    Dec 17, 2013
    Posts:
    178
    Thanks for your answer, I would like to avoid relying on the joystick rotation speed because it might be not reflect to gameobject motion depending on the setup.
    Do you think it's possible to compute the rotation speed on the gameobject ?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Anything's possible.

    If you want to measure the speed or rate of something, you need, generally, two reference points. So if you want to measure how much the player has rotated, record the direction it was headed in during the last frame and the current frame, and say use something like Vector3.SignedAngle to measure the angle. The greater the angle, the faster the player is rotating, and you can use that to angle them.
     
    Blackfire-Studio and SisusCo like this.
  5. Blackfire-Studio

    Blackfire-Studio

    Joined:
    Dec 17, 2013
    Posts:
    178
    Thanks for the help !