Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

How to make one movement direction be equal to diferent cameras?

Discussion in 'Cinemachine' started by karmoon5, Jul 7, 2018.

  1. karmoon5

    karmoon5

    Joined:
    Jul 5, 2018
    Posts:
    2
    Hello,

    I have 4 cameras, each camera represents a player, so, I have player one camera, player two camera, player three camera, and finaly, player four camera. All cameras are lookingAt() same target. I have a UI with two buttons, one to make camera RotateArround() the target, like in a orbit, but only moves in one direction, and the second button its to make camera RotateArround() the target and move it backwards to the initial position in the inverse direction.

    What's the problem? The problem it's that I can rotate with player one camera arround the target but when I change to player two, three or four, the cameras start rotating in wrong directions. This is because every camera as its own rotation and position values, so moving a axis one camera brings diferents results in every camera.

    Examples
    Behaviours:


    Cameras Controll script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class cameraControll : MonoBehaviour
    6. {
    7.  
    8.     public string cameraGroup;
    9.  
    10.     private GameObject lookAtTarget;
    11.  
    12.     private int speed = 0;
    13.    
    14.     private float zoomSmoothness = 0;
    15.     private float minZoom = 0;
    16.     private float maxZoom = 0;
    17.  
    18.     private float maxRotationX = 0;
    19.     private float minRotationX = 0;
    20.  
    21.     public void moveUp()
    22.     {
    23.         if (lookAtTarget == null)
    24.         {
    25.             Debug.LogError("Camera target it's null");
    26.             return;
    27.         }
    28.  
    29.         if (transform.rotation.x > maxRotationX)
    30.         {
    31.             return;
    32.         }
    33.  
    34.         Debug.Log("Current position: " + transform.position + " Current rotation: " + transform.rotation);
    35.         transform.RotateAround(lookAtTarget.transform.position, Vector3.left, Time.deltaTime * speed);
    36.     }
    37.  
    38.     public void moveDown()
    39.     {
    40.         if (lookAtTarget == null)
    41.         {
    42.             Debug.LogError("Camera target it's null");
    43.             return;
    44.         }
    45.  
    46.         if (transform.rotation.x < minRotationX)
    47.         {
    48.             return;
    49.         }
    50.  
    51.         transform.RotateAround(lookAtTarget.transform.position, Vector3.left, Time.deltaTime * speed);
    52.     }
    53.  
    54.     public void zoomIn()
    55.     {
    56.         GetComponent<Camera>().fieldOfView = Mathf.Lerp(GetComponent<Camera>().fieldOfView, minZoom, Time.deltaTime * zoomSmoothness);
    57.     }
    58.  
    59.     public void zoomOut()
    60.     {
    61.         GetComponent<Camera>().fieldOfView = Mathf.Lerp(GetComponent<Camera>().fieldOfView, maxZoom, Time.deltaTime * zoomSmoothness);
    62.     }
    63.  
    64.    
    65.     //Setters && Getters\\
    66.  
    67.     public void setLookAtTarget(GameObject lookAtTarget)
    68.     {
    69.         this.lookAtTarget = lookAtTarget;
    70.     }
    71.  
    72.     public void setSpeed(int speed)
    73.     {
    74.         this.speed = speed;
    75.     }
    76.  
    77.  
    78.     public void setMinZoom(float minZoom)
    79.     {
    80.         this.minZoom = minZoom;
    81.     }
    82.  
    83.     public void setMaxZoom(float maxZoom)
    84.     {
    85.         this.maxZoom = maxZoom;
    86.     }
    87.  
    88.     public void setZoomSmoothness(float zoomSmoothness)
    89.     {
    90.         this.zoomSmoothness = zoomSmoothness;
    91.     }
    92.  
    93.     public void setMaxRotationX(float maxRotationX)
    94.     {
    95.         this.maxRotationX = maxRotationX;
    96.     }
    97.  
    98.     public void setMinRotationX(float minRotationX)
    99.     {
    100.         this.minRotationX = minRotationX;
    101.     }
    102.  
    103.  
    104.     public string getCameraGroup()
    105.     {
    106.         return this.cameraGroup;
    107.     }
    108. }
    109.  
    Any solutions?

    Thank you in advance.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,714
    The direction of rotation is determined by the axis and angle. To rotate the other way, you can either negate the angle or negate the axis (i.e. use either -speed for the angle, or Vector3.right for the axis when you call RotateAround()).
     
    karmoon5 likes this.
  3. karmoon5

    karmoon5

    Joined:
    Jul 5, 2018
    Posts:
    2
    Ok, but I can "automate" that? I have 4 cameras with diferent axis angles ?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,714
    Sorry, but I don't really understand what you want to automate. Your second picture is unclear to me, and seems to contradict the green part of the first picture. Perhaps you could elaborate a little more about what you're trying to achieve.