- Home /
Center Camera view on Center of GameObject [solved]
I'm trying to figure out what I need to write in order to get the camera to center on a gameobject (this case a cube) after the user as clicked on it. I have a script that works close to what I need;however, it goes to the mouse's location.
I'm not sure what do would be best: to twick the raycast script applied to the main camera, or edit it to act with another script attached to gameobjects. I was thinking if I went with the second, it maybe easier since there is more than one cube in scene.
If someone could help me figure out what to do, that would be awesome. And if I need to explain a little more about the project I'm trying to work on, I can do so.
Here's the raycast script I have.
#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); }
@kitty Eventually all the question will be answered and they will be solved. There is no point of including Solved in the title or updating the corrections in the initial question, because the answers will look weird if you can see the initial error for somebody that will encounter similar problem..!
Answer by Eric5h5 · Mar 03, 2010 at 07:53 PM
Instead of setting targetPosition to hit.point, try setting it to hit.collider.transform.position.
Thank you! It works! But any idea why it's running into middle of the cubes and ignoring the box colliders?
You're setting the camera position explicitly, and presumably your camera isn't a rigidbody with a collider, which is the only way that collision events would be generated.
Answer by David 3 · Apr 07, 2010 at 09:43 AM
Surely the line myCamera.position += 0.25*(targetPosition-myCamera.position); should be within the if(Input.GetMouseButtonDow n test otherwise the script will fire contineously and the camera wont follow commands from things like an fps walker script
Answer by David 3 · Apr 07, 2010 at 09:50 AM
I am not sure if i am using this script correctly - the on click seems to hit any mesh and once clicked i seem to rotate about a point ?!
i have attached the script to a mesh and set the my camera variable to the camera of my fps walker camera.
I set this script to an empty gameobject with my camera as the main camera and it's functioning the way I need to.$$anonymous$$ain camera zooms and offsets from a gameobject that I have clicked on. Click again, it resets back to the original starting position.
Since that's the case, I'm not sure how it looks for the fps walker camera...
Your answer
Follow this Question
Related Questions
Using the accelerometer to rotate Camera 1 Answer
How do you write scripts attached to a game object that move the main camera 1 Answer
Camera to follow a target within a circle? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to run an animation mouse click. 2 Answers