Camera Rotation Around Player
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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraOrbit : MonoBehaviour {
//Public Variables\\
public GameObject targetObject;
public Transform target;
public float horizMove = 45f;
public float vertMove = 15f;
//Private Variables\\
private Vector3 positionOffset = Vector3.zero;
//Initiates on first frame\\
void Start ()
{
positionOffset = transform.position - target.transform.position;
}
//Updates once per frame\\
void Update ()
{
if (targetObject != null)
{
transform.LookAt (target.transform);
}
transform.position = target.transform.position + positionOffset;
}
//Movement Setup\\
public void MoveHorizontal (bool left)
{
float dir = -1;
if (!left)
dir *= -1;
transform.RotateAround (target.position, Vector3.up, horizMove * dir * Time.deltaTime);
}
public void MoveVertical (bool up)
{
float dir = -1;
if (!up)
dir *= -1;
transform.RotateAround (target.position, transform.TransformDirection (Vector3.right), vertMove * dir * Time.deltaTime);
}
}
And here is the InputManager.cs Script to allow the camera to rotate using the arrow keys:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputManager : MonoBehaviour {
//Public Variables\\
CameraOrbit cam;
void Start ()
{
cam = GetComponent<CameraOrbit> ();
}
void Update ()
{
if (Input.GetKey (KeyCode.LeftArrow))
{
cam.MoveHorizontal (true);
} else if (Input.GetKey (KeyCode.RightArrow))
{
cam.MoveHorizontal (false);
} else if (Input.GetKey (KeyCode.UpArrow))
{
cam.MoveVertical (true);
} else if (Input.GetKey (KeyCode.DownArrow))
{
cam.MoveVertical (false);
}
}
}
Answer by LiloE · Mar 06, 2017 at 08:25 AM
On line 34, inside your update
function you have the following code:
transform.position = target.transform.position + positionOffset;
This snaps your camera back to position :-)
Your answer
Follow this Question
Related Questions
Making the player the camera's axis of rotation. 0 Answers
Problems with rotating the player on the Y axis C# 0 Answers
How do i limit the rotation of a game object in relation to a free following camera ? 0 Answers
Problems with rotation after zoom (camera like Sketchfab) 0 Answers
No overload for method 'RotateAround' takes one argument 1 Answer