I’m writing a basic camera controller. My general approach was to set the camera as a child of a target so that I could work in local space around the target. This is what I have so far.
#pragma strict
var offset : Vector3 = Vector3(0, 2, -3);
var target : GameObject;
var zoomRate : float = 1.0;
function LateUpdate ()
{
if (transform.parent == null)
{
AcquireTarget(target, offset); //Calls function to set camera as child of target object
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.position.z -= zoomRate;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.position.z += zoomRate;
}
}
function AcquireTarget(functionTarget : GameObject, functionOffset : Vector3)
{
transform.parent = functionTarget.transform; //Sets camera as parent of target
transform.position = target.transform.position + functionOffset; // Puts camera behind target
}
My problem is that when use mouse scroll, both the position.z and the position.x of the camera transform are changing because the camera is not moving on the x-axis in world space. The whole point of setting parenting was to work in local space… so why is changing the transform moving in world space when its a child…