Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED]Camera Rotation issues

Discussion in 'Scripting' started by Browdaddy96, May 28, 2016.

  1. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    So my camera is following a way point system where the camera moves between the two positions and stays focused on the same point. I want the camera once it stops moving to be able to move when the player right clicks, here is my code:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         camStuff.Reset();
    4.         rotStuff.Reset();
    5.  
    6.         rotStuff.mousePos = Input.mousePosition;
    7.  
    8.         camStuff.cameraPos = mainCam.transform.position;
    9.  
    10.         if(Input.mousePosition != rotStuff.mouseOldPos && moving == false && Input.GetMouseButtonDown(1))
    11.         {
    12.             RotateCamera();
    13.         }
    14.  
    15.         if(camStuff.cameraPos != camStuff.oldCameraPos)
    16.         {
    17.             moving = true;
    18.         } else
    19.         {
    20.             moving = false;
    21.         }
    22.  
    23.         if (Input.GetMouseButtonDown(0))
    24.         {
    25.             MoveCamera();
    26.         }
    27.     }
    28.     /*
    29.  
    30. Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
    31. // Translate along world cordinates. (Done this way so we can angle the camera freely.)
    32. transform.position -= new Vector3(touchDeltaPosition.x* PanSpeed, 0, touchDeltaPosition.y* PanSpeed);
    33. */
    34.     void RotateCamera()
    35.     {
    36.         mainCam.transform.rotation = new Vector3(rotStuff.mouseOldPos.x * mouseSensitivity, 0, rotStuff.mouseOldPos.y * mouseSensitivity);
    37.     }
    38.  
    39. struct RotationStuff
    40.     {
    41.         public Vector3 mouseOldPos;
    42.         public Vector3 mousePos;
    43.  
    44.         public void Reset()
    45.         {
    46.             mouseOldPos = mousePos;
    47.             mousePos = Vector3.zero;
    48.         }
    49.     }
    Thank you.
     
  2. crispybeans

    crispybeans

    Joined:
    Apr 13, 2015
    Posts:
    210
    Whats the solution ?
     
  3. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Code (CSharp):
    1. using UnityEngine;
    2. using DG.Tweening;
    3.  
    4. public class CameraWaypoints : MonoBehaviour
    5. {
    6.     public float transitionDuration = 1.0f;
    7.     public Transform[] posiblePositions;
    8.  
    9.     public Transform targetPosForRotation;
    10.  
    11.     public Transform mainCam;
    12.  
    13.     public bool moving = false;
    14.  
    15.     CameraStuff camStuff;
    16.  
    17.     float verticalRotation = 0;
    18.     public float upDownRange = 90.0f;
    19.  
    20.     public float mouseSensitivity = 5f;
    21.  
    22.     void Update()
    23.     {
    24.         camStuff.Reset();
    25.  
    26.         camStuff.cameraPos = mainCam.transform.position;
    27.  
    28.         if(moving == false && Input.GetMouseButton(1))
    29.         {
    30.             float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
    31.             transform.Rotate(0, rotLeftRight, 0);
    32.  
    33.             verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
    34.             verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
    35.             Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
    36.         }
    37.  
    38.         if(camStuff.cameraPos != camStuff.oldCameraPos)
    39.         {
    40.             moving = true;
    41.         } else
    42.         {
    43.             moving = false;
    44.         }
    45.  
    46.         if (Input.GetMouseButtonDown(0))
    47.         {
    48.             MoveCamera();
    49.         }
    50.     }
    51.  
    52.     void MoveCamera()
    53.     {
    54.         int randomSpot = Random.Range(0, posiblePositions.Length);
    55.  
    56.         if(posiblePositions[randomSpot].position != mainCam.transform.position)
    57.         {
    58.             Vector3 destination = posiblePositions[randomSpot].position;
    59.             var lookAtRotation = Quaternion.LookRotation((targetPosForRotation.position - destination).normalized);
    60.  
    61.             mainCam.transform.DOMove(destination, transitionDuration);
    62.             mainCam.transform.DORotateQuaternion(lookAtRotation, transitionDuration);
    63.         } else
    64.         {
    65.             MoveCamera();
    66.         }
    67.     }
    68.  
    69.     struct CameraStuff
    70.     {
    71.         public Vector2 oldCameraPos;
    72.         public Vector2 cameraPos;
    73.  
    74.         public void Reset()
    75.         {
    76.             oldCameraPos = cameraPos;
    77.             cameraPos = Vector2.zero;
    78.         }
    79.     }
    80. }