- Home /
CameraZoom Changeing Position
Hello, I am working with a modified script that moves the camera in front of object that is between camera and player.
#pragma strict
var origin : Transform; // What is considered the origin to the camera
var zoom : float;
var zoomMin : float = -5;
var zoomMax : float = 5;
var seekTime = 1.0;
var smoothZoomIn = false;
private var defaultLocalPosition : Vector3;
private var thisTransform : Transform;
private var currentZoom : float;
private var targetZoom : float;
private var lastTargetZoom : float;
private var zoomVelocity : float;
var myPostionStart : Transform;
var myPositionEnd : Transform;
//var targetGuiText : GUIText;
var cameraItSelf : Transform;
var cameraLookAt : Transform;
var myPostionStartReal : Vector3;
var myPositionEndReal : Vector3;
function Start()
{
thisTransform = transform;
defaultLocalPosition = thisTransform.localPosition;
currentZoom = zoom;
myPostionStartReal = myPostionStart.localPosition;
myPositionEndReal = myPositionEnd.localPosition;
}
function Update()
{
zoom = Mathf.Clamp( zoom, zoomMin, zoomMax );
var layerMask = ~((1 << 8) | (1 << 2));
var hit : RaycastHit;
var zoomedPosition = defaultLocalPosition + thisTransform.parent.InverseTransformDirection( thisTransform.forward * zoom );
var myPostionStartReal = myPostionStart.position;
var myPositionEndReal = myPositionEnd.position;
Debug.DrawLine (myPostionStartReal, myPositionEndReal, Color.red);
if ( Physics.Linecast( myPostionStartReal, myPositionEndReal, hit, layerMask ) && hit.transform.name == "Plane" )
{
var position = hit.point + thisTransform.TransformDirection( Vector3.forward );
var difference = position - thisTransform.TransformPoint( defaultLocalPosition );
targetZoom = difference.magnitude;
}
else
{
targetZoom = 0;
}
targetZoom = Mathf.Clamp( targetZoom, zoomMin, zoomMax );
if ( !smoothZoomIn && ( targetZoom - currentZoom ) > 0 )
{
currentZoom = targetZoom;
}
else
{
currentZoom = Mathf.SmoothDamp( currentZoom, targetZoom, zoomVelocity, seekTime );
}
zoomedPosition = defaultLocalPosition + thisTransform.parent.InverseTransformDirection( thisTransform.forward * currentZoom );
thisTransform.localPosition = zoomedPosition;
}
If i try to rotate the camera (x axis) when it's not re-positioned it works ok.
But when i try to rotate it when it has been re-positioned (the camera is under an object) it changes the position and nothing is correct anymore.
The camera has no rigibody , colliders ...
Does anybody know how to solve this?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613113608im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera follow in c# 1 Answer
Camera to smoothly LookAt() a series of empty objects along a curved path that act as a 'rail' 3 Answers
Making a camera that follows a rigidbodied sphere. 2 Answers
Camera following/looking at aircraft 1 Answer
Can anyone please tell me why this script doesnt work? - Raycasting 1 Answer