Search Unity

Rotating around a pole based on input and screen orientation/direction.

Discussion in 'Scripting' started by rjdfhorn2006, May 30, 2016.

  1. rjdfhorn2006

    rjdfhorn2006

    Joined:
    Jun 14, 2010
    Posts:
    141
    In a nutshell, I'm trying to recreate pole climbing similar to that of Super Mario 64. I have a character who can grab a pole and rotate around it. The player can rotate the camera around the player a full 360 degrees. I've attached a screenshot. Here's my problem - right now I'm rotating around the pole using RotateAround:

    Code (CSharp):
    1.  transform.RotateAround(position, Vector3.up, (lookDir.x * 150f) * Time.deltaTime);
    This works, but doesn't take into consideration the screen orientation.

    Think about it like this: if the camera is directly BEHIND the character and you press left, you would expect the character to rotate towards their left (and towards the screen's left side). If the camera is directly IN FRONT of the character and you press left, you would expect the character to rotate towards their right (and towards the screen's left side - the second image illustrates this).
     

    Attached Files:

  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Have you tried using the Cameras transform.right and -transform.right instead of lookDir.x???
     
  3. rjdfhorn2006

    rjdfhorn2006

    Joined:
    Jun 14, 2010
    Posts:
    141
    Figured it out. I got the angle between the white line and the character's forward vector. lookDir is probably a bad name for this variable - it simply points left or right relative to the camera depending on player input. This snippet handles the case when the player is pressing the left key.

    Code (CSharp):
    1. float angle = Vector3.Angle(transform.right, lookDir);
    2. if (horizontal < 0)
    3.         {
    4.             if(angle > 90)
    5.             {
    6.                 //rotate to character's left
    7.                 transform.RotateAround(position, Vector3.up, 50 * Time.deltaTime);
    8.             }
    9.             else if(angle < 90)
    10.             {
    11.                 //rotate to character's right
    12.                 transform.RotateAround(position, Vector3.up, -50 * Time.deltaTime);
    13.             }
    14.         }
    15.