Camera follow issue.
So, I have a camera that has to follow the player that is a rolling ball, and i have the following script:
public GameObject target;
public Transform targetTransform;
public Rigidbody targetRigidbody;
private Transform camTransform;
private float distance = 20.0f;
private float height = 5.0f;
private float rotationDamping = 0.2f;
private float heightDamping = 10;
void Awake()
{
camTransform = this.gameObject.GetComponent<Transform>();
target = GameObject.FindWithTag("Player");
targetTransform = target.GetComponent<Transform>();
targetRigidbody = target.GetComponent<Rigidbody>();
}
void update(){
//set the starting values
float targetRotation;
float targetHeight = targetTransform.position.y + height;
float camRotation = camTransform.eulerAngles.y;
float camHeight = camTransform.position.y;
Vector3 flatSpeed= targetRigidbody.velocity;
flatSpeed.y = 0; //flat speed of the player
if (move == Vector3.zero)
targetRotation = camRotation;
else //record Y rotation of flat speed if it is moving
targetRotation = Quaternion.LookRotation(move).eulerAngles.y;
camHeight = Mathf.Lerp(camHeight, targetHeight, heightDamping * Time.deltaTime);
camRotation = Mathf.LerpAngle(camRotation, targetRotation, rotationDamping * Time.deltaTime);
Quaternion currentRotation; //get the wanted rotation obtained through lerping
currentRotation = Quaternion.Euler(0, camRotation, 0);
Vector3 wantedPos = targetTransform.position - (currentRotation * Vector3.forward * distance);
wantedPos.y = camHeight;
camTransform.position = wantedPos;
camTransform.LookAt(targetTransform);
//set position and rotation of the camera transform
}
now, this scripts works preatty fine, it follows smoothly the ball from the back while it moves. my only problem is that, when i make the ball roll backward instead of forward the camera rotates 180 degrees and follows the player from the front insted, as you would expect. this gets the game quite confusing, and I really don't know how to fix this script. Or if anyone knows of a better script and could be so kind to post it here. Thank you in advance.
Answer by Castermaluney · May 18, 2016 at 01:48 PM
Ok I finally found out a solution, but I warn you thins only works on a rolling ball behaving like a wheel, like mine (it moves spinning only on its x axis); It's quite convoluted so i'll try to explain: since my ball spins on the on the x axis, I thought that rotating it by 90 degrees would turn it roughly into the "front" of the ball. so i just set as that the rotation of the camera around the ball.
Here's the code:
private Transform playerTransform;
private Transform camTransform;
private float distance = 20.0f;
private float height = 5.0f;
private float rotationDamping = 0.2f;
private float heightDamping = 10;
void Awake()
{
camtransform = gameObject.GetComponent<Transform>();
playerTransform = gameObject.FindWithTag("Player").GetComponent<Transform>();
}
void Update()
{
float currentAngle = camTransform.eulerAngles.y;
float wantedHeight = targetTransform.position.y + height;
Vector3 frontDirection = Quaternion.Euler(0, -90, 0) * targetTransform.right;
frontDirection.y = 0; //flat vector just to be sure
float wantedAngle = Quaternion.LookRotation(frontDirection).eulerAngles.y;
//interpolation to have a damping effect
float targetAngle = Mathf.LerpAngle(currentAngle, wantedAngle, rotationDamping * Time.fixedDeltaTime);
float targetHeight = Mathf.Lerp(camTransform.position.y, wantedHeight, heightDamping * Time.fixedDeltaTime);
//set the camera transform where it should be
Quaternion currentRotation = Quaternion.Euler(0, targetAngle, 0);
Vector3 wantedPos = targetTransform.position - (currentRotation * Vector3.forward * distance);
wantedPos.y = targetHeight;
camTransform.position = wantedPos;
camTransform.LookAt(targetTransform);
}
Hope it helps somebody.
made an even better system: it still follows the speed but it inverts it when you are moving bakcward; haven't found a better system to check if you are moving backward.
private Transform playerTransform;
private Transform camTransform;
private Rigidbody playerRigidbody;
private float distance = 20.0f;
private float height = 5.0f;
private float rotationDamping = 0.2f;
private float heightDamping = 10;
void Awake()
{
camtransform = gameObject.GetComponent<Transform>();
playerTransform = gameObject.FindWithTag("Player").GetComponent<Transform>();
playerRigidbody = playerTransform.gameObject.GetComponent<Rigidbody>();
}
void Update()
{
float currentAngle = camTransform.eulerAngles.y;
float wantedHeight = targetTransform.position.y + height;
float wantedAngle;
Vector3 frontDirection = Quaternion.Euler(0, -90, 0) * targetTransform.right;
frontDirection.y = 0; //flat vector just to be sure
Vector3 flatSpeed = targetRigidbody.velocity;
flatSpeed.y = 0;
float angleCheck = Vector3.Angle(frontDirection, flatSpeed);
if (angleCheck < 90 && flatSpeed != Vector3.zero)
wantedAngle = Quaternion.LookRotation(flatSpeed).eulerAngles.y;
else if (flatSpeed != Vector3.zero)
wantedAngle = Quaternion.LookRotation(-flatSpeed).eulerAngles.y;
else //if the ball isn't moving
wantedAngle = Quaternion.LookRotation(frontDirection).eulerAngles.y;
//interpolation to have a damping effect
float targetAngle = $$anonymous$$athf.LerpAngle(currentAngle, wantedAngle, rotationDamping
Time.fixedDeltaTime);
float targetHeight = $$anonymous$$athf.Lerp(camTransform.position.y, wantedHeight, heightDamping * Time.fixedDeltaTime);
//set the camera transform where it should be
Quaternion currentRotation = Quaternion.Euler(0, targetAngle, 0);
Vector3 wantedPos = targetTransform.position - (currentRotation * Vector3.forward * distance);
wantedPos.y = targetHeight;
camTransform.position = wantedPos;
camTransform.LookAt(targetTransform);
}
it still works only with a wheel like movement.
Answer by Zoogyburger · May 17, 2016 at 09:08 PM
No exactly sure how you want the camera to follow the ball, but here is some very simple code:
public Transform target;
void Update()
{
if (target)
{
transform.position = Vector3.Lerp (transform.position, target.position, 0.1f)+ new Vector3 (0, 0, -10);
}
}
}
Don't forget to set your ball as the target in the inspector!
the problem with this script is that the camera doesn't rotate around as the ball turn's left or right, so it doesn't fix my problem.
Your answer
Follow this Question
Related Questions
Spyro Like Camera Follow 0 Answers
When attaching my custom camera script camera shakes when player starts to move fast. 1 Answer
C# Smooth Follow Space Ship 1 Answer
How to stop the camera when the player has reached the edge of the level? 2 Answers
Why is my camera shaking when I use my chaser script on something? 0 Answers