Search Unity

Having a problem with rotation a camera around a focal point and having x rotation limits

Discussion in 'Scripting' started by The_Retro_Bandit, Mar 3, 2015.

  1. The_Retro_Bandit

    The_Retro_Bandit

    Joined:
    Mar 2, 2015
    Posts:
    3
    So I am making a strategy game and I am working on the camera system first. Panning and zooming were easy but rotating around a focal point is a big pain. Using the scripting reference and past code I was able to code the rotation when you hold the middle mouse button, and that works. The problem is in when I try to set the up and down rotation within a certian threshold to prevent the camera from going upsidedown and through the floor. The game , no matter what i do will free the y input if i try to modify it, and after two hours of banging my head against the wall i decided I might as well ask the forums.

    Here is the camera code

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. public class CameraScript : MonoBehaviour {
    4.  
    5.     public Transform target;
    6.     public float speedx = 50;
    7.     public float speedy = 25;
    8.     public Quaternion input;
    9.     public float zoomSpeed = 5;
    10.     public float miny = 10;
    11.     public float maxy = 80;
    12.     public Vector3 targetPosition;
    13.     public Quaternion targetRotation;
    14.     public float distanceFromFocal = 5;
    15.     public float maxZoom = 1;
    16.     public float minZoom = 15;
    17.     // Use this for initialization
    18.     void Start ()
    19.     {
    20.  
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.         //zooming
    26.         distanceFromFocal -= Input.GetAxis ("Mouse ScrollWheel") * zoomSpeed;
    27.         if (distanceFromFocal < maxZoom)
    28.             distanceFromFocal = maxZoom;
    29.         if (distanceFromFocal > minZoom)
    30.             distanceFromFocal = minZoom;
    31.         //rotating
    32.         if (target && Input.GetMouseButton(2))
    33.         {
    34.             input.x += Input.GetAxis("Mouse X") * speedx * Time.deltaTime ;
    35.             input.y -= Input.GetAxis("Mouse Y") * speedy  * Time.deltaTime ;
    36.  
    37.  
    38.  
    39.  
    40.         }
    41.         if (target)
    42.         {
    43.             targetRotation = Quaternion.Euler (input.y, input.x, 0);
    44.      
    45.             targetPosition = targetRotation * new Vector3 (0, 0, -distanceFromFocal) + target.position;
    46.      
    47.      
    48.      
    49.      
    50.             transform.rotation = targetRotation;
    51.             transform.position = targetPosition;
    52.         }
    53.     }
    54.  
    55.  
    56. }
    and here is the focal point code for panning btw the camera is a child of the focal point.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFocal : MonoBehaviour {
    5.  
    6.     public Vector3 targetPosition;
    7.     public Vector3 targetRotation;
    8.     public float currentInputx;
    9.     public float currentInputz;
    10.     public float panSpeed = 7;
    11.     public Vector3 velocity;
    12.     public float panSmooth = 0.5f;
    13.     public Transform cameraTransform;
    14.     // Use this for initialization
    15.     void Start () {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         Vector3 horizontalInput = new Vector3(Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    23.      
    24.         if (horizontalInput.magnitude > 1)
    25.         {
    26.             horizontalInput.Normalize ();
    27.         }
    28.      
    29.         Vector3 targetHorizontalMovement = horizontalInput;
    30.         targetHorizontalMovement = cameraTransform.rotation * targetHorizontalMovement;
    31.         targetHorizontalMovement.y = 0;
    32.         targetHorizontalMovement.Normalize();
    33.         targetHorizontalMovement *= horizontalInput.magnitude;
    34.         transform.position += targetHorizontalMovement * panSpeed * Time.deltaTime;
    35.  
    36.  
    37.     }
    38. }
     
    Last edited: Mar 3, 2015
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I don't know the solution to the problem, but a workaround is to use the mouse orbit script that comes standard with unity, and modify the controls to work with the zoom. You can set the Y Min limit and Y max.
     
  3. The_Retro_Bandit

    The_Retro_Bandit

    Joined:
    Mar 2, 2015
    Posts:
    3
    I found the solution. For some reason mathf.clamp does not work in the version of unity i am using.