I am actually working on a game where the player will be able to control an airship with keyboard and camera, but not like in FPS where we control a human, where a big mouse drag can lead to an immediate 360° movement. I want the player to follow slowly the camera direction, depending of the vehicle. The best example I can give for this type of movement is vehicle movements in Borderlands 2.
//Code was here, see updated version below
EDIT : I managed to clamp the height rotation using this post, adapting it with my needs. It now do what I want for height rotation, but it is messing with the side rotation… For unknown reason, I can’t go past 90° left or right. I didn’t modify the transform.RotateAround for the side rotation, so it’s something else in this that is causing the problem, but I can’t figure it out.
public Transform player;
float heightRotation;
float sideRotation;
public float sensitivity;
Vector3 initialVector;
void Start () {
sensitivity = 3;
initialVector = transform.position - player.position;
initialVector.x = 0;
}
// Update is called once per frame
void Update () {
//getting mouse inputs
sideRotation = Input.GetAxis ("Mouse X");
heightRotation = Input.GetAxis ("Mouse Y");
//applying method seen on the post linked above, though I don't understand what ".x > 0 ? 1 : -1" is doing..
Vector3 currentVector = transform.position - player.position;
currentVector.x = 0;
float angleBetween = Vector3.Angle(initialVector, currentVector) * (Vector3.Cross(initialVector, currentVector).x > 0 ? 1 : -1);
float upAngle = Mathf.Clamp(angleBetween + (heightRotation * sensitivity * Time.deltaTime), -75, 75);
upAngle = upAngle - angleBetween;
float sideAngle = sideRotation * sensitivity * Time.deltaTime;
//applying calculated angles to the camera
transform.RotateAround(player.position, Vector3.right, upAngle);
transform.RotateAround(player.position, Vector3.up, sideAngle);
transform.LookAt(player);
}
Any clues about what is causing the side rotation unwanted clamping ? Also, it isn’t clamping roughly like it does for the height rotation. As the height angle values are capped, if I try to go above it, nothing happens, the camera isn’t moving at all. For the side rotation, it’s like two forces opposing together, if I move my mouse rapidly, it will go above 90°, but go back when I stop moving my mouse. And last thing, no matter the angle I chose for the clamp, the side rotation will stop at 90°, tried different values from 15 to 120°, working perfectly for height rotation… Thanks in advance !