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 perseushaolysce · Jan 21, 2014 at 10:51 PM · beginnertutorialclick objectsc #

How to Code to Play Animation Only When Object is Clicked (C#)

I'm having trouble with what seems like it should be a very simple problem. I can't seem to find a simple explanation or tutorial showing how to make an object respond when clicked on. I have a C# script that plays an animation when the left mouse button is clicked, but I don't know how to make it only play when the object itself is clicked, not just any place on the screen. I know this must be simple, but I have absolutely zero experience in coding anything, so I really need a simple explanation that I could apply to other objects and scripts as well. Here is my current script:

using UnityEngine; using System.Collections;

public class Shrinkscript : MonoBehaviour {

 // Update is called once per frame
 void Update () {
     if (Input.GetMouseButton(0))
 {
         animation.Play("Shrink");
 }
 
 }

}

Any help is much appreciated!

Comment
Add comment · Show 1
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 Exalia · Jan 21, 2014 at 11:00 PM 0
Share

Raycasting, I will post a solution in a second

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by IndieScapeGames · Jan 24, 2014 at 07:02 PM

Use OnMouseDown() instead. This way if you click only the object that has a collider attached to it, will it play the animation.

 function OnMouseDown () 
 {
     hit.animation.Play("Shrink");
 }
Comment
Add comment · Share
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
0

Answer by Exalia · Jan 21, 2014 at 11:02 PM

This will try to play the animation "Shrink" on anything you click on, the object needs a collider.

 if(Input.GetMouseButton(0))
 {
  Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  RaycastHit hit;
  
  if(Physics.Raycast(ray, out hit, 100))
  {
   hit.animation.Play("Shrink");
  }
 }
Comment
Add comment · Show 1 · Share
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 perseushaolysce · Jan 24, 2014 at 06:48 PM 0
Share

I tried this code and it tells me "parsing error." I'm not sure what's wrong.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Help with selecting and deselecting an object? 1 Answer

Best starter tutorial 3 Answers

How can I start unity fast with my C++ engine 2 Answers

Scripts that aren't in an object 2 Answers


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