Scripting beginner - how can I make the camera follow the character but still face the same direction?
I have a script that follows the character, however it rotates with the character, which is quite annoying since the character appears to be moving forward, but I am actually holding the left arrow key.
C# Smooth Follow script:
using UnityEngine;
namespace UnityStandardAssets.Utility
{
public class SmoothFollow : MonoBehaviour
{
// The target we are following
[SerializeField]
private Transform target;
// The distance in the x-z plane to the target
[SerializeField]
private float distance = 10.0f;
// the height we want the camera to be above the target
[SerializeField]
private float height = 5.0f;
[SerializeField]
private float rotationDamping;
[SerializeField]
private float heightDamping;
// Use this for initialization
void Start() { }
// Update is called once per frame
void LateUpdate()
{
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x ,currentHeight , transform.position.z);
// Always look at the target
transform.LookAt(target);
}
}
}
There is also a LookAt script on the character, which makes the camera look at the character. With the smooth follow script, looks both at the character and moves with it.
JavaScript script for LookAt:
#pragma strict
var target: GameObject;
function Update(){
transform.LookAt(target.transform.position);
}
Answer by NightLucididty · Nov 24, 2015 at 09:27 AM
If you just want the camera to follow the player and keep the same direction all you need is to make the player a parent of the camera. Of course there will be no smooth follow, but from what I can tell from your question you don't want that either
Answer by CptBolee · Nov 24, 2015 at 03:07 PM
You should put maincamera as a child of player.. but also you should lock axis that rotates.. If its a 3d game it should be z... if not try y axis.. Don't know exactly.. I don't have acces to a computer now.. I'll check later and post it.. If you succed let me know ;) Best regards
Edit: if you want to use script to do that you can write..
transform.rotation = Quaternion.... (new Vector3 (0f,0f,0f));
or what ever you current rotation is... if your camera is rotated ex. 30 degrees on X and -20 on Y Than just write (30f, -20f, 0f)