- Home /
Camera Zoom in&out
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!
Answer by etopsirhc · Jul 23, 2017 at 01:09 AM
you'll need a second vector3 as the return position. var camRet : vector3;
then you should set this just after line 21 camRet = camTr.position;
then after line 31 you will need an else { camPos = camRet; }
that should get the click outside to zoom out done, but to zoom out using right click you will need to see if it's zoomed in already with var zoomed : bool;
and after line 24 a zoomed = true;
need to check do a if(Input.GetMouseButtonDown(1) && zoomed){ camPos = camRet; zoomed = false; }
before line 34.
Answer by eMPi1984 · Jul 23, 2017 at 05:29 AM
Thank you very much. I also tried to fix it and it worked. I just changed
if (go == hit.collider.gameObject) {
camPos.x = go.transform.position.x;
camPos.y = go.transform.position.y;
camPos.z += 5;
to:
if (go == hit.collider.gameObject) {
camPos.x = go.transform.position.x;
camPos.y = go.transform.position.y;
camPos.z = -10;
and added
if (Input.GetMouseButtonDown(1)) {
camPos.z = -20;
}
The first change did "disable" to zoom on every click as i wanted to that the cam only zooms one time to one final position. The 2nd change simply zooms out to its previous state. Simple changes but it worked well for me. As said, I'm a beginner at coding.
I updated my first posts code.
etopsirhc's way looks much better and more professional then mine, I'll try this one out for my next step.
Thank you!
Your answer
Follow this Question
Related Questions
Start raycast from object 1 Answer
cast a ray to hit an object in the center of the screen 1 Answer
Is object at least partly visible? 1 Answer
Script not function if mouse is on object 1 Answer
Move object to raycast point. 3 Answers