Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question How do I rotate this?

Discussion in 'Scripting' started by alainjiehfeng, Jun 15, 2020.

  1. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class VirtualCamController : MonoBehaviour
    5. {
    6.     private Controls controls;
    7.     public Transform virtualcam;
    8.     private Vector2 vector2;
    9.  
    10.     void Awake()
    11.     {
    12.         controls = new Controls();
    13.     }
    14.  
    15.     void OnEnable()
    16.     {
    17.         controls.Enable();
    18.     }
    19.  
    20.     void OnDisable()
    21.     {
    22.         controls.Disable();
    23.     }
    24.  
    25.     void LateUpdate()
    26.     {
    27.         virtualcam.Rotate(GetAxisCustom("Mouse X"), GetAxisCustom("Mouse Y"), 0f);
    28.     }
    29.  
    30.     public float GetAxisCustom(string axisname)
    31.     {
    32.         vector2 = controls.AllControls.Camera.ReadValue<Vector2>();
    33.  
    34.         if (axisname == "Mouse X")
    35.         {
    36.             return vector2.x;
    37.         }
    38.  
    39.         if (axisname == "Mouse Y")
    40.         {
    41.             return -vector2.y;
    42.         }
    43.  
    44.         return 0;
    45.     }
    46. }
    At the moment this is rotating the camera all over including all axes. I just want to rotate the camera transform only by the X and Y rotation, but this is also doing the Z. I need to clamp as well I think, could anyone help me edit this please? :)
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,778
    The easiest way to do this is to not use .Rotate, which adds the value you supply to its current rotation. The issue is just geometry: If you rotate right 90 degrees, then up 90 degrees, then left 90 degrees, you'll end up facing the same way you started, but with a 90 degree roll. This is where your phantom Z rotation is coming from.

    If you fully overwrite the rotation every frame, you won't get this problem. With your code, you can do that with:
    Code (csharp):
    1. virtualcam.rotation = Quaternion.Euler(GetAxisCustom("Mouse X"), GetAxisCustom("Mouse Y"), 0f);
    2.  
    3. ...
    4.  
    5. vector2 += controls....
    This basically moves the "cumulative" aspect of the rotation to the Vector2 instead of the quaternion, where it won't combine in strange ways.

    By the way, I think you should be swapping your x and y? When you move the mouse X, usually you want the object to rotate on the Y axis (around the vertical axis, so it's like shaking your head "no"), and vice versa. I don't know the rest of your input setup though, so maybe this is swapped elsewhere.
     
    alainjiehfeng likes this.
  3. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Hi, thanks for that. I think I get what you mean by going around all the way and coming back to the same point.

    And yes I had to change both the X and Y and it looks like this now:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class VirtualCamController : MonoBehaviour
    5. {
    6.     private Controls controls;
    7.     public Transform virtualcam;
    8.     private Vector2 vector2;
    9.  
    10.     void Awake()
    11.     {
    12.         controls = new Controls();
    13.     }
    14.  
    15.     void OnEnable()
    16.     {
    17.         controls.Enable();
    18.     }
    19.  
    20.     void OnDisable()
    21.     {
    22.         controls.Disable();
    23.     }
    24.  
    25.     void LateUpdate()
    26.     {
    27.         virtualcam.rotation = Quaternion.Euler(GetAxisCustom("Mouse X"), GetAxisCustom("Mouse Y"), 0f);
    28.         //virtualcam.Rotate(GetAxisCustom("Mouse X"), GetAxisCustom("Mouse Y"), 0f);
    29.     }
    30.  
    31.     public float GetAxisCustom(string axisname)
    32.     {
    33.         vector2 += controls.AllControls.Camera.ReadValue<Vector2>();
    34.  
    35.         if (axisname == "Mouse X")
    36.         {
    37.             return -vector2.y;
    38.         }
    39.  
    40.         if (axisname == "Mouse Y")
    41.         {
    42.             return vector2.x;
    43.         }
    44.  
    45.         return 0;
    46.     }
    47. }
    48.  
    Currently it does the rotation but not quite where I want it. Is it possible to have a starting rotation by default and then modify it with the controls? Also how do I add restrictions of going too far with clamping? :)
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    If I understand what you are trying to do, the easiest way to do it is child the camera. Rotate the parent on one axis and the camera on the other.
     
    alainjiehfeng likes this.
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,778
    Yep, you can assign a default value to your vector2 variable on line 2. You could also make that public and tweak the starting value in the inspector.

    Code (csharp):
    1. vector2.x = Mathf.Clamp(vector2.x, -45f, 45f);
     
    alainjiehfeng likes this.
  6. alainjiehfeng

    alainjiehfeng

    Joined:
    May 13, 2020
    Posts:
    60
    Thank you! I will try that next time, sounds easy.

    Thanks! I got it to work somehow. But right now I am faced with a new problem, how do I get this virtual cinemachine camera to also follow the player around and not have the rotations affected by it?

    I tried the Look At and Follow, but that overrides the transform:



    The position in particular. I can't have my own position.

    I also tried parenting it to the player, but my player rotates around all the time which is how my game goes, so I can't have the camera rolling as well. Any ideas?

    EDIT: Nvm, I just realized I can set the position to the player's position, and then add an offset of choice to get it where I want it. Thanks again.
     
    Last edited: Jun 15, 2020