- Home /
Click on collider to rotate camera
Hi everyone. I have come across a problem where I can't get my camera to rotate when I click on a collider. I want to be able to click on a collider and have the camera rotate to look at a different collider. I am trying to create a more "interactive" menu but so far it has gone nowhere as I cannot seam to fix this. I'm new to scripting, so if possible it would be nice to get any ideas in JavaScript.
What you already have now? Can you post it here? Be more precise in your description, get my camera to rotate when I click on a collider can mean any of these 3 (or perhaps more):
Click on collider, camera rotate and look at collider
Click on collider, camera rotate in a specific axis
Click on collider, camera rotate randomly (shaking?)
Sadly I don't have anything at the moment (as I said I'm new to scripting) but click on collider, camera rotate and look at collider fits what I want most.
Answer by Chronos-L · Apr 13, 2013 at 09:39 AM
Combine these 3:
C# example
void Update() {
if( Input.GetMouseButtonDown(0) ) { //Left Click
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if( Physics.Raycast( ray, out hit ) ) {
Camera.main.transform.LookAt( hit.transform );
}
}
}
Can you explain what happens in this, because I don't really understand C# very well. Thanks
void Update() {
//Detect for a left mouse click
if( Input.Get$$anonymous$$ouseButtonDown(0) ) {
//Create a ray that is ai$$anonymous$$g at where your mouse is ai$$anonymous$$g
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//This will store the raycast information
RaycastHit hit;
//return true when the ray hit something, store information in the hit-variable
if( Physics.Raycast( ray, out hit ) ) {
//call the lookat function of the camera's transform to point/lookAt the hit gameObject
Camera.main.transform.LookAt( hit.transform );
}
}
}
Your answer
Follow this Question
Related Questions
Slowly orbit camera on keypress 1 Answer
Rotate an Object 1 Answer
Camera scripting references 0 Answers
Set Max Rotation On Weapon Sway 0 Answers
Limiting Camera Movement 1 Answer