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

Camera Rotation around Player at a set offest?

Discussion in 'Scripting' started by LovelyVibes, Mar 5, 2017.

  1. LovelyVibes

    LovelyVibes

    Joined:
    Feb 7, 2017
    Posts:
    16
    So I have some code put in place currently trying to get my camera to rotate around my player in this case a Sphere. My issue is that all of the code is set in place for it to do just that. However when I go to rotate using the arrow keys it jerks back to the original rotation position. Any help would be greatly appreciated! ^~^

    Here is the CameraOrbit.cs Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraOrbit : MonoBehaviour {
    6.  
    7.     //Public Variables\\
    8.  
    9.     public GameObject targetObject;
    10.     public Transform target;
    11.     public float horizMove = 45f;
    12.     public float vertMove = 15f;
    13.  
    14.     //Private Variables\\
    15.  
    16.     private Vector3 positionOffset = Vector3.zero;
    17.  
    18.     //Initiates on first frame\\
    19.  
    20.     void Start ()
    21.     {
    22.         positionOffset = transform.position - target.transform.position;
    23.     }
    24.  
    25.     //Updates once per frame\\
    26.  
    27.     void Update ()
    28.     {
    29.         if (targetObject != null)
    30.         {
    31.             transform.LookAt (target.transform);
    32.         }
    33.  
    34.         transform.position = target.transform.position + positionOffset;
    35.     }
    36.  
    37.     //Movement Setup\\
    38.  
    39.     public void MoveHorizontal (bool left)
    40.     {
    41.         float dir = -1;
    42.         if (!left)
    43.             dir *= -1;
    44.         transform.RotateAround (target.position, Vector3.up, horizMove * dir * Time.deltaTime);
    45.     }
    46.  
    47.     public void MoveVertical (bool up)
    48.     {
    49.         float dir = -1;
    50.         if (!up)
    51.             dir *= -1;
    52.         transform.RotateAround (target.position, transform.TransformDirection (Vector3.right), vertMove * dir * Time.deltaTime);
    53.     }
    54. }
    55.  
    And here is the InputManager.cs Script to allow the camera to rotate using the arrow keys:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InputManager : MonoBehaviour {
    6.  
    7.     //Public Variables\\
    8.  
    9.     CameraOrbit cam;
    10.  
    11.     void Start ()
    12.     {
    13.         cam = GetComponent<CameraOrbit> ();
    14.     }
    15.  
    16.     void Update ()
    17.     {
    18.         if (Input.GetKey (KeyCode.LeftArrow))
    19.         {
    20.             cam.MoveHorizontal (true);
    21.         } else if (Input.GetKey (KeyCode.RightArrow))
    22.         {
    23.             cam.MoveHorizontal (false);
    24.         } else if (Input.GetKey (KeyCode.UpArrow))
    25.         {
    26.             cam.MoveVertical (true);
    27.         } else if (Input.GetKey (KeyCode.DownArrow))
    28.         {
    29.             cam.MoveVertical (false);
    30.         }
    31.     }
    32. }
     
    Last edited: Mar 5, 2017
  2. booiljoung

    booiljoung

    Joined:
    May 12, 2013
    Posts:
    57
    Hi there!

    It is difficult to read because your code is centered.

    Cheers!
     
    LovelyVibes likes this.
  3. LovelyVibes

    LovelyVibes

    Joined:
    Feb 7, 2017
    Posts:
    16
    I fixed the alignment my apologies I did not realize it was centered. ^~^
     
  4. LovelyVibes

    LovelyVibes

    Joined:
    Feb 7, 2017
    Posts:
    16
    Could someone mark this thread as closed, I am not aware of how to do so but I have figured out the issue I was having. I decided to go the route of Raycasting with the camera and a target. Thank you for the help. ^~^