- Home /
Help fixing a bug with camera script
Hey, I'm a Unity noobie working on my first project and I've run into an interesting problem with it. The game is just a simple motorcycle that you can drive around. It goes forward with input from the vertical axis (like W/S or Up/Down Arrow) and turns the bike with input from the vertical axis (A/D or Left/Right). I've been working on a script for the camera and this is what I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public float dstFromTarget = 12f;
public float smoothTime = 0.5f;
public float camHeight = 3f;
public float camAngle = 5f;
private Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
private void LateUpdate()
{
SmoothFollow();
}
void SmoothFollow()
{
Vector3 targetPos = target.transform.eulerAngles;
Vector3 camPos = transform.eulerAngles;
currentRotation = Vector3.SmoothDamp(currentRotation, targetPos, ref rotationSmoothVelocity, smoothTime);
transform.eulerAngles = currentRotation + new Vector3(camAngle, 0f, 0f);
transform.position = target.position + new Vector3 (0f, camHeight,0f) - (transform.forward * dstFromTarget);
}
}
The script works great almost all the time. It follows the bike like I want it to as I drive except for one problem. It freaks out when the rotation goes from 360 to 0 or vice versa in the Y-direction. I'm kind of stumped on how I might fix this problem because I don't know how to make 360 to 0 a smooth transition. If anyone has any solutions, I'd love to hear them. Thanks in advance!
Answer by unity_ek98vnTRplGj8Q · Mar 13, 2020 at 02:58 PM
Instead of smoothing your Euler angles I recommend smoothing the entire rotation. Quaternion.Slerp will rotate smoothly from one rotation to another without having to worry about angle overflow.
Quaternion cameraTilt = Quaternion.Euler(camAngle, 0 ,0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetPos * cameraTilt, speed * Time.deltaTime);
Maybe something like this
Answer by Flash1Lucky · Mar 14, 2020 at 10:42 PM
Awesome, that worked! Thanks a lot for the help. I don't feel like I understand quaternions as much as I would like to but I guess I understand it enough for now because I was able to get it to work. Here's the latest code I used for archive purposes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public float dstFromTarget = 12f;
public float speed = 5f;
public float camHeight = 3f;
public float camAngle = 5f;
private void LateUpdate()
{
SmoothFollow();
}
void SmoothFollow()
{
Quaternion targetPos = target.rotation;
Quaternion cameraTilt = Quaternion.Euler(camAngle, 0, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetPos * cameraTilt, speed * Time.deltaTime);
transform.position = target.position + new Vector3(0f, camHeight, 0f) - (transform.forward * dstFromTarget);
}
}
Thanks again for the help! @unity_ek98vnTRplGj8Q
Your answer
Follow this Question
Related Questions
Weird lighting bugs 1 Answer
rotate with alt doesnt work 15 Answers
Editor Camera "cuts off" objects/terrain 2 Answers
Depth Fade Problem on distance in VR 0 Answers