- Home /
How can I get my camera to reset its position behind the player?
I am making a game where the player controls a spaceship and I wanted to add a smooth follow camera. I tried some code, but I am having some trouble. Whenever I tilt the ship down, the camera zooms out and the opposite when I tilt the ship up. Whenever I raise the position damping, the camera no longer moves smoothly with the ship. If anyone could help me with this that would be greatly appreciated. Here is my code:
using UnityEngine; using System.Collections;
public class CameraFollow : MonoBehaviour { public Transform target; // The target we are following public float distance; // The distance from the target along its Z axis public float height; // the height we want the camera to be above the target public float positionDamping; // how quickly we should get to the target position public float rotationDamping; // how quickly we should get to the target rotation
// Use this for public variable initialization
public void Reset() {
distance = 1.75f;
height = 0.5f;
positionDamping = 10f;
rotationDamping = 80f;
}
// LateUpdate is called once per frame
public void LateUpdate () {
ensureReferencesAreIntact();
#region Get Transform Manipulation
// The desired position
Vector3 targetPosition = target.position + target.up * height - target.forward * distance;
// The desired rotation
Quaternion targetRotation = Quaternion.LookRotation(target.position-transform.position, target.up);
#endregion
#region Manipulate Transform
transform.position = Vector3.MoveTowards(transform.position, targetPosition, positionDamping * Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationDamping * Time.deltaTime);
#endregion
}
// Checks to make sure all required references still exist and disables the script if not
private void ensureReferencesAreIntact() {
if (target == null) {
Debug.LogError("No target is set in the SmoothFollow Script attached to " + name);
this.enabled = false;
}
}
}
Answer by Graphics_Dev · Mar 14, 2016 at 03:32 PM
Here is a clean solution for camera follow. Add a camera as a child of the player located to have the exact view you want. Disable the camera component on this object. Add another camera to your scene and add this script. Drag the other camera to it's transform property in the inspector. Hit play and watch the magic!
Use this code:
using UnityEngine;
using System.Collections;
public class SmoothCamFollow : MonoBehaviour
{
public Transform target;
private new Transform camera;
public float posSpeed = 1.0F;
public float rotSpeed = 1.0F;
// Use this for initialization
void Start ()
{
camera = GetComponent<Transform> ();
}
// Update is called once per frame
void Update ()
{
// position movement
camera.position = Vector3.Lerp (camera.position, target.position, (posSpeed * Time.deltaTime));
// rotation movement
camera.rotation = Quaternion.Lerp (camera.rotation, target.rotation, (rotSpeed * Time.deltaTime));
}
}
Your answer
Follow this Question
Related Questions
Need help/advice with 2D Camera follow script. 1 Answer
Camera Follow sphere with ridgidbody? 4 Answers
Camera Smooth Follow 2 Answers
Having issues with a smooth camera follow 2 Answers
Smooth Camera 2D 0 Answers