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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Interpolate rotation between two objects

Discussion in 'Scripting' started by electro_unity, Jan 26, 2018.

  1. electro_unity

    electro_unity

    Joined:
    Nov 29, 2015
    Posts:
    64
    Hello, Im having troubles trying to make my player look at another object. Player is a flying vehicle so it rotates on axis X and Y, with the following code

    Code (CSharp):
    1.  
    2.             if ((leftVerticalInput == 1 && rotX< 50) || (leftVerticalInput == -1 && rotX> -50))
    3.             {
    4.                 leftVerticalRotationSpeed = 40;
    5.  
    6.                 rotX+= leftVerticalInput * Time.deltaTime * leftVerticalRotationSpeed;
    7.             }
    8.             else
    9.             {
    10.                 leftVerticalRotationSpeed = 0;
    11.             }
    12.  
    13.  
    14.             if (leftVerticalInput == 0 && rotX> 2.5f)
    15.             {
    16.                 leftVerticalRotationSpeed = 30;
    17.  
    18.                 leftVerticalInput = -0.5f;
    19.                 rotX+= leftVerticalInput * Time.deltaTime * leftVerticalRotationSpeed;
    20.             }
    21.             else if (leftVerticalInput == 0 && rotX< -2.5f)
    22.             {
    23.                 leftVerticalRotationSpeed = 30;
    24.  
    25.                 leftVerticalInput = 0.5f;
    26.                 rotX+= leftVerticalInput * Time.deltaTime * leftVerticalRotationSpeed;
    27.             }
    28.  
    29.        
    30.             transform.Rotate(0, leftHorizontalInput * Time.deltaTime * leftHorizontalRotationSpeed, 0, Space.World);
    31.  
    32.  
    33.  
    34.             transform.Rotate(leftVerticalInput * Time.deltaTime * leftVerticalRotationSpeed, 0, 0, Space.Self);
    35.  
    As you can see, Im saving the X axis rotation on a variable in order to clamp this axis rotation. But in certain cases (facing a treasure or an enemy), I need to make player look at some object (smoothly) and later return the rotation control to the player. The problem is that using transform.rotate and a lerp function, or look at, or anything I can think of, make me loose the control of rotX variable, so when the control returns to the player, this variable is outdated and no longer working properly. Any idea? Thanks.
     
  2. cjdev

    cjdev

    Joined:
    Oct 30, 2012
    Posts:
    42
    What is the rotX variable for exactly? It seems like you might be able to instead calculate the offset of the local forward vector to the player's forward vector and use that instead. This way you wouldn't have to store the change in rotation and could switch control of the player's view around more easily.
     
    electro_unity likes this.
  3. electro_unity

    electro_unity

    Joined:
    Nov 29, 2015
    Posts:
    64
    I use rotX to keep track of player rotation on X axis. When it reaches 50 or -50 I can stop player rotation on that axis. I tried using vector and quaternion calculations between original and current transform.forward, in order to get the difference angle and stop player if reaches 50/-50. And it works fine if I only rotate on X axis, but the problem is when player rotates Y axis too. Then the angle difference on X axis is not the same.