Rotating camera problem C# Android


I’m having a problem whereby the camera keeps rotating when my spaceship rolls left/right as you turn left/right. Is there a way to lock a cameras rotatation to a set point or to only allow it to follow and not rotate…here is my script:

~EDIT~ Further info: The spaceship travels upwards along the Y axis and is controlled by the device’s tilt function to move it left and right. I want the camera to follow the ship along the X & Y axis (2D movement only) and no rotation of the camera at all.

using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {

public Transform target;
public float smooth = 0.3F;
public float distance = 80.0F;
private float yVelocity = 0.0F;

void Update() {
	float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y, ref yVelocity, smooth);
	Vector3 position = target.position;
	position += Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance);
	transform.position = position;


Thanks :slight_smile:

If I understand you correctly, you want a camera that follows a fixed distance behind your spaceship, always points at the spaceship, and pitches and yaws with the spaceship, but doesn’t roll with it.

How about something like (pseudo-code):

//pitch adjusts x euler angle, yaw adjusts y euler angle, roll adjusts z euler angle
//we need to undo the roll to get the desired up vector.
Vector3 unrotatedUp = target.up rotated around target.forward by target.eulerAngles.z
Vector3 cameraUp = //do some smoothing here between unrotatedUp and transform.up 
transform.position = target.position + target.forward * -distance;
transform.LookAt(target, cameraUp);

Obviously I’ve not tested any of this, so expect some bugs. :slight_smile:

Great blog !It is best institute.Top Training institute In Coimbatore
Pragmatic Play Daftar Slot Gacor RTP Tinggi Terpercaya Hari Ini
Pragmatic Play Daftar Slot Gacor RTP Tinggi Terpercaya Hari Ini
Pragmatic Play Daftar Slot Gacor RTP Tinggi Terpercaya Hari Ini