Search Unity

rotate around object in X axis

Discussion in 'Scripting' started by Aizat-Musa, Sep 28, 2018.

  1. Aizat-Musa

    Aizat-Musa

    Joined:
    Feb 25, 2016
    Posts:
    8
    Hi guys, I need some help with rotate around function.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. [System.Serializable]
    7. public struct CameraMoveSetting
    8. {
    9.     public string name;
    10.     public bool moveX;
    11.     public bool moveY;
    12.     public bool moveZ;
    13.     public float minX,maxX;
    14.     public float minY,maxY;
    15.     public float minZ,maxZ;
    16.     public float moveSpeed;
    17.     public bool rotateX;
    18.     public bool rotateY;
    19.     public bool rotateZ;
    20.     public float rotateSpeed;
    21.     public float rightRotateX, leftRotateX;
    22.     public float downRotateY, upRotateY;
    23.     //public float rightRotateZ, leftRotateZ;
    24.     public Transform rotatePoint;
    25. }
    26.  
    27.  
    28. public class CameraMovement : MonoBehaviour {
    29.  
    30.     public CameraMoveSetting[] camMoveSetting;
    31.     public CameraMoveSetting currentSetting;
    32.     public bool foundMovement;
    33.     public bool moveTo;
    34.     public GameObject cam;
    35.     //public Transform centerPoint;
    36.     //public float RotateSpeed;
    37.     float minDistance ;
    38.     float  maxDistance ;
    39.     //public Transform rotatePoint;
    40.     public Vector3 currentMovePoint;
    41.     public Quaternion currentRotatePoint;
    42.     public Vector3 initialPos;
    43.     float yaw = 0f;
    44.     float pitch = 0.0f;
    45.  
    46.     private Vector3 m_TargetAngles;
    47.     private Vector3 m_FollowAngles;
    48.     private Vector3 m_FollowVelocity;
    49.     //private Quaternion m_OriginalRotation;
    50.     //public Vector2 rotationRange = new Vector3(70, 70);
    51.     public float dampingTime = 0.2f;
    52.  
    53.     float rotX = 0f;
    54.     float rotY = 0f;
    55.     private Vector2 currentRotation;
    56.     public Transform child;
    57.     // Use this for initialization
    58.     void Start () {
    59.         SetCurrentCamera("Main");
    60.        
    61.     }
    62.    
    63.     // Update is called once per frame
    64.     void Update () {
    65.        
    66.     }
    67.  
    68.     public void SetCurrentCamera(string name)
    69.     {
    70.         foreach (CameraMoveSetting move in camMoveSetting) {
    71.  
    72.             if (move.name == name) {
    73.                 currentSetting.name = move.name;
    74.                 currentSetting.moveX = move.moveX;
    75.                 currentSetting.moveY = move.moveY;
    76.                 currentSetting.moveZ = move.moveZ;
    77.                 currentSetting.minX = move.minX;
    78.                 currentSetting.maxX = move.maxX;
    79.                 currentSetting.minY = move.minY;
    80.                 currentSetting.maxY = move.maxY;
    81.                 currentSetting.maxY = Vector3.Distance(move.rotatePoint.position, cam.transform.position);
    82.                 currentSetting.minZ = move.minZ;
    83.                 currentSetting.maxZ = move.maxZ;
    84.  
    85.                 currentSetting.rotateSpeed = move.rotateSpeed;
    86.                 currentSetting.rotateX = move.rotateX;
    87.                 currentSetting.rotateY = move.rotateY;
    88.                 currentSetting.rotateZ = move.rotateZ;
    89.                 currentSetting.rightRotateX = move.rightRotateX;
    90.                 currentSetting.leftRotateX = move.leftRotateX;
    91.                 currentSetting.upRotateY = move.upRotateY;
    92.                 currentSetting.downRotateY = move.downRotateY;
    93.                 currentSetting.moveSpeed = move.moveSpeed;
    94.                 currentSetting.rotatePoint = move.rotatePoint;
    95.                 initialPos = cam.transform.position - currentSetting.rotatePoint.position;
    96.                 foundMovement = true;
    97.                 break;
    98.             } else {
    99.                 foundMovement = false;
    100.             }
    101.  
    102.         }
    103.  
    104.     }
    105.  
    106.     public void CheckCameraMovement()
    107.     {
    108.        
    109.  
    110.         if (!moveTo) {
    111.  
    112.             if (!foundMovement)
    113.                 return;
    114.  
    115.             if (currentSetting.rotatePoint != null && !EventSystem.current.IsPointerOverGameObject()) {
    116.  
    117.                 if (cam != null && Input.GetMouseButton (0) ) {
    118.  
    119.  
    120.  
    121.                     rotX = Input.GetAxis("Mouse X") * currentSetting.rotateSpeed ;
    122.                     rotY = Input.GetAxis("Mouse Y") * currentSetting.rotateSpeed ;
    123.  
    124.  
    125.  
    126.                     if (currentSetting.rotateX)
    127.                     {
    128.  
    129.                         Vector3 newInitial = initialPos;
    130.                         newInitial.y = 0f;
    131.                         Vector3 dirX;
    132.                         dirX = cam.transform.position - currentSetting.rotatePoint.position; //find current direction
    133.                         dirX.y = 0f;
    134.                         var angleX = Vector3.Angle(newInitial, dirX);
    135.                         if (Vector3.Cross(newInitial, dirX).y < 0)
    136.                             angleX = -angleX;
    137.                         var newAngleX = Mathf.Clamp(angleX + rotX, -currentSetting.rightRotateX, currentSetting.leftRotateX);
    138.                         rotX = newAngleX - angleX;
    139.                        // Debug.Log("rotX" + rotX);
    140.                         cam.transform.RotateAround(currentSetting.rotatePoint.position, Vector3.up, rotX);
    141.  
    142.                     }
    143.                     if (currentSetting.rotateY)
    144.                     {
    145.  
    146.  
    147.                         Vector3 newInitial = initialPos;
    148.                         Vector3 dirY;
    149.                         newInitial.x = 0f;
    150.                         dirY = cam.transform.position - currentSetting.rotatePoint.position; //find current direction
    151.                         dirY.x = 0f;
    152.                         var angleY = Vector3.Angle(newInitial, dirY);
    153.                         if (Vector3.Cross(newInitial, dirY).x < 0)
    154.                             angleY = -angleY;
    155.  
    156.                         var newAngleY = Mathf.Clamp(angleY - rotY, -currentSetting.upRotateY, currentSetting.downRotateY);
    157.                         rotY = newAngleY - angleY;
    158.  
    159.                          Debug.Log("angleY:" + angleY + "  newAngleY: " + newAngleY + "  rotY: " + rotY);
    160.  
    161.                        
    162.                         child.transform.RotateAround(currentSetting.rotatePoint.position, Vector3.right, rotY);
    163.  
    164.  
    165.                     }
    166.  
    167.  
    168.  
    169.                 } else if (cam != null && Input.GetMouseButton (1)) {
    170.  
    171.                     float rotX = Input.GetAxis("Mouse X") * currentSetting.rotateSpeed * Mathf.Deg2Rad;
    172.                     float rotY = Input.GetAxis("Mouse Y") * currentSetting.rotateSpeed * Mathf.Deg2Rad;
    173.  
    174.                     if (currentSetting.moveY) {
    175.                         Vector3 newPos = Vector3.MoveTowards(cam.transform.position, currentSetting.rotatePoint.position, rotY * currentSetting.moveSpeed);
    176.                         float distance = Vector3.Distance(newPos, currentSetting.rotatePoint.position);
    177.                         if (distance > currentSetting.minY && distance < currentSetting.maxY)
    178.                         {
    179.                             cam.transform.position = newPos;
    180.                         }
    181.  
    182.  
    183.                     }
    184.  
    185.  
    186.                 }
    187.  
    188.             }
    189.  
    190.         } else {
    191.             MovingTo (currentMovePoint,currentRotatePoint);
    192.  
    193.         }
    194.  
    195.     }
    196.  
    197.     void MovingTo(Vector3 pos,Quaternion rot)
    198.     {
    199.  
    200.         //&& Quaternion.Dot(cam.transform.rotation,currentRotatePoint) > 0.999f
    201.         if (Vector3.Distance (cam.transform.position, pos) > 0.01f) {
    202.             cam.transform.position = Vector3.Lerp (cam.transform.position, pos, 5f * Time.deltaTime);
    203.             cam.transform.rotation = Quaternion.Lerp (cam.transform.rotation, rot, 5f * Time.deltaTime);
    204.         } else {
    205.             moveTo = false;
    206.  
    207.             initialPos = cam.transform.position - currentSetting.rotatePoint.position;
    208.  
    209.         }
    210.  
    211.     }
    212.  
    213.  
    214. }
    215.  


    Ok, what my code do is when we click and move the mouse it will rotate around a target accordingly. there is no problem rotating the y axis (move the mouse left and right) but the problem is on the x axis(mouse up and down). this is the part when the code doesnt work

    Code (CSharp):
    1.  if (currentSetting.rotateY)
    2.                     {
    3.  
    4.  
    5.                         Vector3 newInitial = initialPos;
    6.                         Vector3 dirY;
    7.                         newInitial.x = 0f;
    8.                         dirY = cam.transform.position - currentSetting.rotatePoint.position; //find current direction
    9.                         dirY.x = 0f;
    10.                         var angleY = Vector3.Angle(newInitial, dirY);
    11.                         if (Vector3.Cross(newInitial, dirY).x < 0)
    12.                             angleY = -angleY;
    13.  
    14.                         var newAngleY = Mathf.Clamp(angleY + rotY, -currentSetting.upRotateY, currentSetting.downRotateY);
    15.                         rotY = newAngleY - angleY;
    16.  
    17.                          Debug.Log("angleY:" + angleY + "  newAngleY: " + newAngleY + "  rotY: " + rotY);
    18.  
    19.                        
    20.                         cam.transform.RotateAround(currentSetting.rotatePoint.position, Vector3.right, rotY);
    21.  
    22.  
    23.                     }
    what I found out is for this particular line
        cam.transform.RotateAround(currentSetting.rotatePoint.position, Vector3.right, rotY);
    that i can not use
    Vector3.right
    particularly for X axis rotation because of a rule to "Pitch Locally, Yaw Globally" then i change to this
         cam.transform.RotateAround(currentSetting.rotatePoint.position, cam.transform.right, rotY);
    so it would rotate around local X axis but what happened is the clamping for does not work anymore. so is there any fault in my clamping code? also if there's a way I can rotate around both X and Y axis at the same time with different clamping rule for both axes?