- Home /
Mouse Orbit snapping issues
Hello, I was wondering how to possibly fix a problem we have been having with the mouse orbit script. Here's the situation:
We have a cannon that rotates and aims up and down using the mouse look scripts. When it has been aimed, clicking the left mouse button fires a sphere with a rigidbody attached from the end of the cannon barrel. Right now we have it so the main camera is initially a child of the cannon so that it's looking down the barrel whenever it's rotated, but when the sphere is fired, we unparent it from the cannon and set the sphere as the target for the main camera in the Mouse Orbit script. The only issue is that when this occurs, the camera snaps to an initial side view instead of just snapping forward a bit to be behind the ball following it through the air.
We did mess with the Mouse Orbit script a bit and now it only snaps to a TOP down view whenever the barrel of the cannon is not horizontal with the ground (i.e. it is being fired at an angle into the air).
We were wondering if anyone else has had this issue before and what could be done to fix the script for this purpose.
Here is a video that shows the issue. I have also provided the slightly modified script below.
var target : Transform;
var snapPoint : Transform;
var canFollow : boolean;
var firstSet : boolean;
var distance = 40.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 () {
canFollow = false;
firstSet = true;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target && canFollow) {
if(firstSet){
firstSet = false;
transform.rotation = snapPoint.rotation;
transform.position = snapPoint.position;
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
else{
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);
}
function SetFollow(other : Transform){
canFollow = true;
}
Your answer
Follow this Question
Related Questions
Touch mouseorbit 5 Answers
Mouse Orbit snapping issues 0 Answers
Need help on the 3ds max style camera control 0 Answers
Mouse.position from center of player 1 Answer
help with my orbit camera script 0 Answers