I’ve made a camera that follow x, y, and z player position, and rotation. But i’ve a problem,
when playership rotate, the camera updates his rotation but not his relative position (i want my camera to keep the same distance between itself and the playership and to keep the same angle) So when i rotate the playership, the angle between my camera and the playership is wrong. Is there a way to calculate the new x, y and z coordinates of the camera using player rotation and default offset ?
Thank in advance
Hi,
Create a C# script call “TransformFollower.cs” and copy paste this code :
using UnityEngine;
using System.Collections;
public class TransformFollower : MonoBehaviour
{
[SerializeField]
private Transform target;
[SerializeField]
private Vector3 offsetPosition;
[SerializeField]
private Space offsetPositionSpace = Space.Self;
[SerializeField]
private bool lookAt = true;
private void Update()
{
Refresh();
}
public void Refresh()
{
if(target == null)
{
Debug.LogWarning("Missing target ref !", this);
return;
}
// compute position
if(offsetPositionSpace == Space.Self)
{
transform.position = target.TransformPoint(offsetPosition);
}
else
{
transform.position = target.position + offsetPosition;
}
// compute rotation
if(lookAt)
{
transform.LookAt(target);
}
else
{
transform.rotation = target.rotation;
}
}
}
Drag and drop your player Transform into the “target” script.
Don’t forget to add an offset value, like (0, 5, -12).
You can always make the camera a child of the player object and set it at some distance behind the player (to make it like a 3d person game). But you have to be aware of clipping and this kind of stuff.
Why don’t you just give the camera a fixed joint component and put the player as its connected body?
try SmoothFollow Built in script
I have a working answer to this, a bit like @Walack 's answer
This is for smooth moving if you want, but if you don’t want smooth moving just follow one of the last paragraphs I have
So to start, you will need to make two empty gameobjects, both childs of the player. One should be named “Camera Goal Location” and the other “Camera Goal Lookat”
On the camera, add this script:
Just a note that the CameraCurrentLookAt should be changed to a position that you want the camera to start looking at
public GameObject CameraGoalLookAt;
public GameObject CameraGoalPosition;
public Vector3 CameraCurrentLookAt;
public float MovementDividor;
public float LookAtDividor;
void LateUpdate() {
// Camera Movement
vector3 movement = CameraGoalPosition - transform.position;
movement = movement / MovementDividor;
transform.position += movement;
// Camera Look At
vector3 movementA = CameraGoalLookAt - CameraCurrentLookAt;
movementA = movementA / LookAtDividor;
CameraCurrentLookAt += movementA;
transform.lookat(CameraCurrentLookAt);
// If you dont' want the camera to have a smooth panning / turning one, then just replace the camera look at code with:
// transform.lookt(CameraGoalLookAt);
}
MovementDividor and LookAtDividor all represent the speed that the camera will pan… Higher values will result in a slower camera, but at any distance, the camera will pan at the same speed, meaning that at a given distance, the camera will move the same speed no matter how far the original distance was (just a thing i noticed)
In essence, the script makes the camera move faster in relation to the distance to the goal position, this I find easily manageable as you can try moving the cameragoalposition around in the editor during testing play and see the camera respond accordingly. The goalposition should be set to the offset that you want under the player
IF you want the camera to have little to no panning, just set the dividors to “1”, and your set…
if you want very very little panning, you can also set it to 1.5 or 2…
once I do that, is there a code to move the camera with wasd but without using horizontal or vertical because for some reason it wont let me use the horizontal OR vertical code