First off, to be clear I’m using JavaScript.
So I created a script that allows me to reset the Y-axis of the main camera when pressing a button. I spent all day today trying to figure out why it didn’t work, and I believe now after messing with a simple project (and enabling/disabling the Mouselook or Mouse Orbit scripts) that it’s the orbit script that prevents me from resetting the camera’s position.
I don’t quite understand how the mouse orbit works, so perhaps I’m just going about this the wrong way.
Anyway, here’s my reset camera script(ResetCam.js):
#pragma strict
var Cam : GameObject;
var originalYPos : float;
function Start () {
originalYPos = 25.0;
}
function Update () {
if (Input.GetButtonDown("f")) {
print(transform.eulerAngles.y);
Cam.transform.eulerAngles.y = originalYPos;
print("after:" + " " + transform.eulerAngles.y);
}
}
With the Mouselook script enabled on my camera, I can rotate freely (but it doesn’t orbit the player, which is what I need.) With Mouse Orbit script on, I can rotate around the player, but when trying to press the above input button to reset the y-axis rotation back to it’s initial value, I get nothing.
My guess is that the orbit script has priority over the camera’s rotation and nothing I do will allow me to actually see the camera reset when pressing my input button. I put a “print” line in my script so I could see if it was actually getting changed, and it is, but you can’t see the change with Mouse Orbit on, only with Mouse Look.
So my question is, is there something I’m missing? Is there a way to allow the camera to still orbit the player, but when pressing a keyboard key the camera’s rotation on the Y-axis gets reset?
Also, I apologize if this has been discussed prior, but I did research on resetting the camera and couldn’t find anything that helped.
I’m really trying to add this as a ‘center camera’ feature for a game I’m working on, much like other 3d games where you can center the camera via button push.
I’ve created a video demonstration that shows an example of what I’m going for, although it’s on the vertical axis rather than the horizontal axis:
Also, here’s the MouseOrbit script:
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}