The problem I have is that when I use the camera orbit script, and another script (which is used to change the materials of an object), the camera automatically zooms into the object.
I am using this script for the camera…
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var distanceMin = 3;
var distanceMax = 15;
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 (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
}
function LateUpdate () {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
var hit : RaycastHit;
if (Physics.Linecast (target.position, transform.position, hit)) {
distance -= hit.distance;
}
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);
}
and this for the material changes…
var materials : Material[]; //specify array for materials
var count = 0; //specify variable for count and set to 0
function Update () {
GetComponent.<Renderer>().material = materials[count]; //assign material from array
if (Input.GetMouseButtonDown(0)) // check for left-mouse
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (GetComponent.<Collider>() && GetComponent.<Collider>().Raycast (ray, hit, 100.0))
{
OnMouseDown(); //call onMouseDown command
}
}
} //end function update
function OnMouseDown ()
{
if (count == materials.Length - 1) //if count as reached the last number in the array
{
count = 0; //then make count = 0
}
else
{
count++; //else add 1 to the count
}
}
On the mesh, which the material change script is applied, is also a mesh collider component. If I disable the mesh collider, the camera orbit works perfectly, and if I turn off the camera orobit script, the material change script works perfectly. I’m guessing there’s a conflict here somewhere, but I’m by no means a programmer, so any help would be really appreciated!
Thanks, Dean