In my game i want the mouse to orbit an object so i use the unity standard script Mouse Orbit, and i added a few modifications. I want the mouse wheel to change the distance. The script already has distance as variable and i just added some code with the mouse wheel which doesnt seem to work. Here is the code:
var target : Transform;
var minDistance : float = 10.0;
var maxDistance : float = 10.0;
var distanceSpeed : float = 1;
//These are the variables i added.
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
private var distance = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
distance = (minDistance + maxDistance)/2;
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
}
function Update () {
}
function LateUpdate () {
if(Input.GetAxis("Mouse ScrollWheel") != 0){
Debug.Log("Scroller detected");
distance += Input.GetAxis("Mouse ScrollWheel") * distanceSpeed * Time.deltaTime;
//I get the log ingame so that means the if statement works but the distance is not changing at all...
}
if(distance > maxDistance) {distance=maxDistance;}
if(distance < minDistance) {distance=minDistance;}
if (target && Input.GetKey(KeyCode.Mouse1)) {
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);
}
There are some mistake in the modified verions of orbit script that you made.
1)When you scroll,the distance variable is getting updated but it doesnt write to the camera Z position.
Since this line var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
is inside the if condition it doesnt update all the time
2)There is no need for Time.deltatime for updating the distance valriable in the scroll detection block
3)You set the minDistance and maxDistance both the same value 10 which will lock the scroll/zoom value to 10.
Here is the corrected code.
var target : Transform;
var minDistance : float = 2.0;
var maxDistance : float = 10.0;
var distanceSpeed : float = 1;
//These are the variables i added.
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
private var distance = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start ()
{
distance = (minDistance + maxDistance)/2;
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
}
function LateUpdate ()
{
//If target exists
if (target)
{
//If scroll occurs make change in distance
if(Input.GetAxis("Mouse ScrollWheel") != 0)
{
distance += Input.GetAxis("Mouse ScrollWheel") * distanceSpeed * -1;
}
//Clamp the distance between maximum and minimum values
distance=Mathf.Clamp(distance,minDistance,maxDistance);
//If RMB is pressed and moved,capture the values to x and y
if(Input.GetKey(KeyCode.Mouse1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
}
//Clampy value between maximum and minimum values
y = ClampAngle(y, yMinLimit, yMaxLimit);
//Make change in rotation
var rotation = Quaternion.Euler(y, x, 0);
//Make change in position
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
//Assign the position and rotation
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);
}
Hope this helps…