Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Norham · Feb 17, 2015 at 06:09 PM · camerabuttonscript.

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fariel · Feb 18, 2015 at 08:52 AM 0
Share

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

http://docs.unity3d.com/ScriptReference/Input.html

http://docs.unity3d.com/ScriptReference/Animation.html

avatar image alok-kr-029 · Feb 18, 2015 at 09:11 AM 1
Share

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");
     }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I attach a GameObject when the script is attached to unity's on click () function (for buttons). 2 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges