- Home /
Move / Play Animation on Camera when i click a 3D (object) Button
I have an Object as a button, i want to move the camera to another position when i click the "New Game" button. I have the animation for the camera, but i do not know how to setup the script for this kind of event.
It seems like you want to cast a ray from a point on the screen when the player clicks. Then, if it hits the "button", you preform the animation.
Something along the lines of:
//if the player clicks with the left mouse button
if(Input.Get$$anonymous$$ouseButtonDown(0)){
//create our ray from the screen where the current mouse position is
Ray clickRay = Camera.main.ScreenPointToRay(Input.mousePosition);
//set up our ray cast hit variable that the raycast will store its information in when we cast it
RaycastHit rayHit;
//preform our raycast. IF AND ONLY IF this ray hit something, do we continue
if (Physics.Raycast (clickRay, out rayHit)){
//since we're here, the ray hit something. Check if that's our "button"
if(rayHit.transform.name == "Button Object Name"){
//if it is our button, execute our movement.
Run$$anonymous$$ovementFunction();
}
}
}
The movement function would hold the call to wherever you camera is. It would tell the animator to play your animation. While the code above is fully functioning, the following code is "approximation" code to put you down the right path:
void Run$$anonymous$$ovementFunction(){
cameraObject.animation.Play();
}
Valuable links are: http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
You can also use On$$anonymous$$ouseDown() but the script has to be attached to the gameobject which you want to set as clickable .
void On$$anonymous$$ouseDown()
{
Debug.log("This object is clicked");
}
Your answer
Follow this Question
Related Questions
Script incompatible with unity 5.3. Please Help! 1 Answer
How do I tell a button to move my camera!? 1 Answer
Changing Cameras With One Button 4 Answers
Ramp Offset with Button press 1 Answer