Camera Rotation stay behind player and not flip

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);
}

I’m not sure if it fixes your problem, but try to relocate your game in just one set of coordinates. I mean so that your ship’s coordinates never went below 0.
The thing you explain might happen because your basic scene is at (0, 0, 0). Set it to like (1000, 1000, 1000) or just set borders for 0 coordinates so that you couldn’t fly over those.

Hi thanks for your response, I’ve tried your suggestion but the camera still flips in front of the player. I’ve switched to using the smooth follow camera which seems to give me what i’m looking for but I cant seem to stop the camera flipping.

Any ideas?