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
6 Answers
6Hi,
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).
Why don’t you just give the camera a fixed joint component and put the player as its connected body?
Yup! Ive made the asset (as far as all the coding) now I am just drawing the different transitions effects, then put it in the asset store. Should be up this weekend or next weekend, depending on when I have time. Ill message you when It is on the asset store.
– DugelStudiosWell I'm done with the asset! It hasn't been accepted into the asset store yet but my promo video is up: https://www.youtube.com/watch?v=JwcjsqDHu0c
– DugelStudiosYou 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.
Of course. You just have to find out the indices of the vertices. Either store the indices for each tile, or calculate them manually; if you use a for(x... for(y... nested loop to create the mesh, then the vertex indices will be equal to the number of tiles created thus far (x*y) times 4 or 6 (depending on the number of vetices each tile has).
– Chernotry 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
Hello ! Here's a script: https://gist.github.com/romainPechot/57918ca4ddfb5b7718ef429d7fb41eb7 Create a new script in your project called "CameraController". Open it in your text editor. Replace everything inside it with my code. Save the changes and go back inside Unity. Add my script as a new Component on your scene's Camera. Select a target (using the first field of my script). I've added the attribute [ExecuteInEditMode], so you should see your camera rotate, even outside the play mode. Cheers !
– Opotable
And of course, apply this script to your camera :) Nice script.
– WalackThank you really !
– creekorfulThis is great! Thanks! But is there a way to have it so you can orbit the camera around the player with the right mouse button? I'd like the camera to follow, but you can change what's "forward".
– Classic-NiallSo, i copy and pasted this code like you said and did all that but for some reason when i try to move my player the camera stays a fixed distance but it rotates in really fast circles around the sphere but never goes to the front side, but you cant see whats going on cause it freaks out, anyone know how to help? I'm making a roll a ball game just so you know.
– swebre2340Nice Script
– adilpwr11