# Camera Orbit zooms in automatically when using another script for material changes

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;

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

code tags… sticky at the top of the scripting forum shows how to use them… please use them.

what language is this written in?

GetComponent.()

doesn’t look right for c# (something.GetComponent()) or js (something.GetComponent(Rigidbody))…?

edited for code tags, sorry

both scripts are JavaScript, and basically butchered from found scripts and past projects.

Thanks,
Dean

huh, the js documentation for GameObject.GetComponent() doesn’t mention that syntax, but the Component.GetComponent() does… c# is the same for both

I don’t see any conflicts in the code of the two scripts (as I’ve proved so far js isn’t exactly “my thing” though)… anything else on these objects? other scripts etc.

Only thing I’ve spotted is that I don’t think

``````if(GetComponent.<Collider>()&&GetComponent.<Collider>().Raycast(....)) {}
``````

is going to save you from null ref errors when there isn’t a collider component present, both parts will be evaluated separately and then combined for the “&&” with the right side throwing an error. If you want to check for a component’s existence you’ll have to nest the if’s.

``````if(GetComponent.<Collider>())
{
if(GetComponent.<Collider>().Raycast(...))
{
// stuff
}
}
``````

Does anyone have any more clues as to what might be the problem, or perhaps a solution around this?