- Home /
Rotating camera problem C# Android
Greetings!
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;
transform.LookAt(target);
}
}
Thanks :)
Answer by rho21 · May 23, 2012 at 11:00 AM
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. :)
Not as complicated as that :) it only needs to follow the ship upwards on the y axis, the distance and anglew should be the same but no rotation, i only want the ship to roll when manouvering. $$anonymous$$y game is a bit like doodle jump but with a spaceship ins$$anonymous$$d.
O$$anonymous$$, so a 2D scrolling game.
Does one of your worldspace axes point out of the screen constantly, or does worldspace ever rotate? Does the camera need to rotate at all, or does it just slide along vertically, keeping pace with the ship?
In the simplest case, you never need to adjust the camera's rotation, and its position should just be (0, target.position.y, -distance) or similar.
If it's something a bit more complicated, you might want to try defining an up vector for the transform.LookAt in your code (though watch out for nasty effects when the target is along this vector from your camera).
Answer by manimarancs · Mar 18, 2020 at 10:27 AM
Great blog !It is best institute.Top Training institute In Coimbatore http://glimtechnologies.com/aws-training-coimbatore/
http://glimtechnologies.com/angular-js-training-coimbatore/
http://glimtechnologies.com/dotnet-training-coimbatore/