Hello everybody,
I’m new here and absolute Beginner at coding.
I’m trying to write a script for my Main Camera to Zoom in when i select my object. After that, I want the camera to zoom out if i click elsewhere or the right mousebutton so that the camera goes back to the previous state.
I’ve found a snippet of a code: (created Tag named “Planet” for my Object)
UPDATE:
#pragma strict
var camPos : Vector3;
var camTr : Transform;
var speed = 2.5;
function Start() {
camTr = Camera.main.transform;
camPos = camTr.position;
}
function Update() {
if (Input.GetMouseButtonDown(0)) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit) && hit.collider.tag == "Planet") {
var Planet = GameObject.FindGameObjectsWithTag("Planet");
for (var go : GameObject in Planet) {
if (go == hit.collider.gameObject) {
camPos.x = go.transform.position.x;
camPos.y = go.transform.position.y;
camPos.z = -10;
}
}
}
}
if (Input.GetMouseButtonDown(1)) {
camPos.z = -20;
}
camTr.position = Vector3.Lerp(camTr.position, camPos, Time.deltaTime * speed);
}
Problems:
- It should have one final zoom position and should not go on with zooming after leftclicking.
- There is no “zoom back” option (rightclick or click elsewhere).
Can somebody help me please?
Thanks in advance!