- Home /
Help with camera follow script with dampening
As the title suggests, I want a camera script that follows the player, in my case, a Spaceship. I have a basic script, but the problem is that I am using Quaternion.LookRotation(), and I think its because of that, it centers my player to the center of the screen, while I want the spaceship to be offset slightly to the bottom of the screen. Also, if I use the rigidbody to move my spaceship, the camera starts stuttering, and I think rigidbody is better than transform.position, as it is better at collision detection, right?
My Code (Camera Follow):
using System;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
private Transform myTransform;
public Vector3 offset;
public float distDampening;
public float rotDampening;
private void Awake()
{
myTransform = transform;
}
private void Update()
{
myTransform.position = Vector3.Lerp(myTransform.position, target.position + (target.rotation * offset), Time.deltaTime * distDampening);
Quaternion desiredRot = Quaternion.LookRotation(target.position - myTransform.position, target.up);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, desiredRot, rotDampening * Time.deltaTime);
}
}
Any help to solve the problems mentioned above is greatly appreciated!!
Answer by DCordoba · Jan 23, 2019 at 12:51 AM
lets try a complete script, in this case we will copy the rotation of the target
using System;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
private Transform myTransform;
public Vector3 offset;
public float distDampening;
public float rotDampening;
private void Awake()
{
myTransform = transform;
}
private void Update()
{
float PosBuffer = target.position + (myTransform.forward * offset.y +
myTransform.right * offset.x -
myTransform.up * offset.z);
myTransform.position = Vector3.Lerp(myTransform.position, PosBuffer, Time.deltaTime * distDampening);
myTransform.rotation = Quaternion.Lerp(myTransform.rotation, target.rotation, rotDampening * Time.deltaTime);
}
}
if u want a fixed pointing on center of screen (used with fixed crosshair) u can sync both at 100 units the forward of target
private void Update()
{
float PosBuffer = target.position + (myTransform.forward * offset.y +
myTransform.right * offset.x -
myTransform.up * offset.z);
myTransform.position = Vector3.Lerp(myTransform.position, PosBuffer, Time.deltaTime * distDampening);
myTransform.LookAt(target.forward*100, target.up);
}
}
if u want a wise pointing of different target distances ever at the center (crosshair precise) of screen u will need start using Raycasting
private void Update()
{
float PosBuffer = target.position + (myTransform.forward * offset.y +
myTransform.right * offset.x -
myTransform.up * offset.z);
myTransform.position = Vector3.Lerp(myTransform.position, PosBuffer, Time.deltaTime * distDampening);
RaycastHit Hitinfo;
if (Physics.Raycast (target.position, target.forward, out Hitinfo, 5000f)){
myTransform.LookAt(Hitinfo.point, target.up);
}else
{
//using the last solution
myTransform.LookAt(target.forward*100, target.up);}
}
}
hope this finally solve all your problems
Of course!! I am really stupid! I was still using Quaternion.LookRotation!! Thanks a lot @DCordoba !
Can you turn this comment or the previous comment into an answer so I can accept it??
@Spacejet56 glad you solved it!
well I suggest consider put a if condition to check if the camera arent already close to desired position, and evade recalculate the same position...
credits to @xxmariofer who help us to reach the solution.
Answer by xxmariofer · Jan 22, 2019 at 09:46 PM
Hello, i dont understand what do you want to do with this line of code
myTransform.position = Vector3.Lerp(myTransform.position, target.position + (target.rotation offset), Time.deltaTime distDampening);
Why are you mixing target.rotation? if you want to offset the position just do target.position + offset rather than multiply the rotation, or is there for some sort of different interactuation?
its a common error, he want to convert the global space to target space, this is the fist (wrong) approach what I did when I started with the same problem, after refreshing my geometry and a bit more tries I got this solution,
float PosBuffer = target.position + (myTransform.forward * offset.y +
myTransform.right * offset.x -
myTransform.up * offset.z);
myTransform.position = Vector3.Lerp(myTransform.position, PosBuffer, Time.deltaTime * distDampening);
I suppose to (not tested) he can use myTransform.InverseTransformDirection(offset);
too
well thats a much nicer approach :) but should you just remove the target.position from inside the lerp method since the posBuffer already takes the target.position into account?
$$anonymous$$y Camera is a separate object, separate from the spaceship, i.r its not parented to the spaceship. Due to this, when my spaceship rotates, my camera's position needs to update, so that it can still be at the back of the spaceship. Otherwise, the camera will stay in the same position even if the spaceship rotates from side to side, which is not good.
Yes i imagine that but thats not the correct way of doing it, imagine your object is not rotated, you will be multiplying the offset * transform.rotation (that will be zero) and will give you zero, so if your object has no rotation you wont have any offset, can you test @DCordoba solution?
I tried that, but the camera starts wildly spinning around the target, the spaceship, and totally freaks out..
Answer by bennett_apps · Jan 22, 2019 at 09:46 PM
As for your first problem, just lerp to target.position + offset...set offset in inspector to something like (0, 5, -6) so it follows that far from the player.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Will someone please help me??? 2 Answers
Object move on z axis while giving force on x axis 0 Answers
How to predict orbit based on time? 1 Answer