Hi,
I’m pretty new to unity development and have been developing a rocket ship that takes off vertically and as it ascends it rotates to fly forwards horizontally. All of this works in my script, but I have a problem getting my camera to track my ship and stay behind it.
The ship is flying in a 3D type game rather than 2D.
My ideal is a camera that is damped, following directly behind the ship, and when the ship turns in any direction the camera follows the back of the ship turning and looking forwards all of the time following in its path.
I have tried making the camera a child of my player, which is fine to some extent but without any script it is very rigid, however the big draw back is that as soon as the ship takes off and rotates to horizontal, the camera moves above it when I want it to stay behind it.
As the game will be mobile based I wanted to avoid using any form of mouse script or additional joystick to allow the player to move the camera where he wants to look, as this will be very difficult to play with the other control buttons in the game.
I have tried using smooth follow, which again is great, but as soon as the rocket rotates below 180 degrees, the camera flips in front of it which again isnt how I would like to to work.
I have attached the following script to my camera which is targetted at my player but currently it will not follow behind the player, but just looks at it. I’ve tried so many different things im a little at a loss as where to go with this next.
Has anyone got any suggestions as to how I can make this work?
public GameObject target;
Vector3 offset;
public float damping = 1;
private float rotx = 0.0f;
private float roty = 0.0f;
void Start()
{
offset = transform.position - target.transform.position;
}
// Update is called once per frame
void Update () {
}
void LateUpdate()
{
Vector3 desiredPosition = target.transform.position + offset;
Vector3 position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime * damping);
transform.position = position;
transform.LookAt(target.transform.position);
}