- Home /
Camera Point-and-click Movement
I'm a noob at scripting and haven't seen what I would like to be able to do. Or maybe I have and it hasn't dawned on me....
The idea is to use a script applied to many different cubes that allows a cube to be clicked. When clicked the camera should snap to a location directly in front of that cube. When clicked again, the camera should snap back to its initial location.
I've figured out how how to get a screen cross fade to work between 2 cameras; however, I need to be able to use just the main camera. Any help would be greatly appreciated.
Thanks.
Answer by Brian-Kehrer · Jan 26, 2010 at 04:24 PM
This is the bit of code I think you want: EDIT: Hacked to solve your comment.
var myCamera : Transform; var targetPosition : Vector3; var zoomedIn : boolean; var defaultPosition : Vector3;
function Update () { if(Input.GetMouseButtonDown(0)){ if(zoomedIn){ targetPosition = defaultPosition; zoomedIn = false; } else{
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray,hit, 50)) {
Debug.Log(hit.point);
targetPosition = hit.point;
zoomedIn =true;
}
}
}
myCamera.position += 0.25*(targetPosition-myCamera.position);
}
From RaycastHit, you can detect which object you have hit, and also get the point of the hit, allowing you to move the camera.
I thought that might work; however, it's not quite right.
I've got the project designed layed out kinda like the periodic table, so I'm trying to make it more of a learning activity program more than a ga$$anonymous$$g. The user should be allowed to click on an object and the camera move in to see certain information then be allowed to click on it again to see a full view of the layout.
Since there's more than one box of information, I remember seeing something about interpolation for the position and direction of the movement. $$anonymous$$aybe something along there would be the best approach?
This just gets you the correct position or object that the user clicked on - interpolation would be different, ill post a quick sample
I am also trying to do something similar, but I'm having difficulty precisely controlling the camera movement. It's shaky in parts. I want the user to click on certain objects in 3D space and be "zip lined" to that location. It would be A$$anonymous$$AZING if you could post a sample. Thanks!
I'm like $$anonymous$$elissa. I like how it goes to where you click; however, I'm looking for something that'll make it go to the center of that object despite where the user clicks. I've put this project aside to work on other things, so I haven't had time to figure it out. Hopefully I'll pick back up on it later this week.
Answer by Kitty · Mar 05, 2010 at 02:53 PM
I had help in another one of my questions, but here's the code that I'm working with applied to the camera that will offset from the object clicked.
#pragma strict
var myCamera : Transform; var targetPosition : Vector3; var zoomedIn : boolean; var defaultPosition : Vector3; var camOffset : Vector3;
function Start(){ targetPosition = myCamera.position; }
function Update () { if(Input.GetMouseButtonDown(0)){ if(zoomedIn){ targetPosition = defaultPosition; zoomedIn = false; }
else{ var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray,hit, 50)) { //targetPosition = hit.point; var hitPosition = hit.transform.position; targetPosition = hitPosition+camOffset; zoomedIn =true; }
}
}
myCamera.position += 0.25*(targetPosition-myCamera.position); }