Hey guys, I’m trying to get my camera to zoom in on the target (my player) when the player collides with a box collider for a dramatic effect. The problem is, nothing happens! I can’t seem to figure out, but it is getting late, maybe someone can help
Here’s my code (JS)
Attach this to a camera, create a capsule or other as a player+rigidbody, and use a cube collision box with a rigid body and with “Is Trigger” checkmarked.
var target : Transform; //Target to follow
var cameraDistance : float; //Distance from target in the z-axis
var cameraDistance2 : float;
var damping : float; //Damping on the movement of the camera
var maxCameraSpeed : float; //Max speed of camera
var limitCameraSpeed : boolean;
private var velocity : Vector3; //Camera velocity used by the SmoothDamp function
private var t : Transform; //Reference to the transform component of the game object
function Awake() {
t = transform; //Cache transform component
}
function LateUpdate() {
if(target) { //If there is a target
var maxSpeed = (limitCameraSpeed) ? maxCameraSpeed : Mathf.Infinity; //Set max speed depending on limitCameraSpeed boolean
t.position.x = Mathf.SmoothDamp(t.position.x, target.position.x, velocity.x, damping, maxSpeed); //Damp x-movement (horizontal follow)
t.position.y = Mathf.SmoothDamp(t.position.y, target.position.y + cameraDistance2, velocity.y, damping, maxSpeed); //Damp y-movement (vertical follow)
t.position.z = Mathf.SmoothDamp(t.position.z, target.position.z - cameraDistance, velocity.z, damping, maxSpeed); //Damp z-movement (camera distance)
}
}
function OnTriggerEnter (other : Collider) {
cameraDistance = 3;
}