- Home /
How do I make my camera follow the player while also following its rotation?
My camera is able to rotate relative to the player's position but it was stay in that rotated position and not move until I press the button to make it move.. How do I make my camera be dynamic by rotating to face the player from all 4 directions while also be dynamic by following the player's movements.
Edit: I've tried using offset scripts but it doesn't and just cuases my camera to do 360s while staying behind my player
my camera script for calculating the math
using UnityEngine;
using System.Collections;
public class CameraControll : MonoBehaviour
{
public Transform target;
public float horizMove = 45f;
public float VertMode = 15f;
public void MoveHorizontal(bool left)
{
float dir = 1;
if (!left)
dir *= -1;
transform.RotateAround(target.position, Vector3.up, horizMove * dir);
}
public void MoveVertical(bool right)
{
float dir = 1;
if (!right)
dir *= -1;
transform.RotateAround(target.position, transform.TransformDirection(Vector3.right), VertMode * dir);
}
}
my script for the inputs
using UnityEngine;
using System.Collections;
public class InputManager : MonoBehaviour
{
CameraControll cam;
void Start()
{
cam = GetComponent<CameraControll>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
cam.MoveHorizontal(true);
}
else if(Input.GetKeyDown(KeyCode.RightArrow))
{
cam.MoveHorizontal(false);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
cam.MoveVertical(true);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
cam.MoveVertical(false);
}
}
}
Answer by ProtoTerminator · Dec 18, 2016 at 04:07 PM
If you want it to always follow your player perfectly, simply set the player as the camera's parent (drag and drop it in the hierarchy). That way you never have to change its position, only its rotation (with RotateAround
. If you want a delay-follow, set up a dummy transform like you would the camera previously, and then every frame have the camera lerp its position and rotation to the dummy object Vector3.Lerp(camera.transform.position, dummy.transform.position, 0.5f)
well the problem is when I make my camera child of my sphere the camera rotates like crazy and when I use the typical offset( offset = transform.position - player.transform.position lateupdate() transform.position = player.transform.position + offset;) the camera just flies into the air
Are you using local or global positions and rotations?
whats the difference between local and global positions .-. ? I'm new so please elaborate