Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jonahsrocket · May 25, 2016 at 08:23 PM · animationclick objects

Trigger animation of one game object by clicking on another game object

Hi everybody,

I have an game object with a script attached. If I click the game object, it's animation will play (it's a puppet walking to the left). Now I want the puppet (game object) to walk to the left if I click another game object.

Is it possible to use the same code and attach it to the other game object and then tweak the code a bit? Or do I have to do this in a different way.

This is the code I use now. Thank you for answering!!!!

 using UnityEngine;
 using System.Collections;
 
 public class Walk : MonoBehaviour
 {
     void Update()
     {
               if (Input.GetMouseButtonDown(0))
         {
             
             Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             
             RaycastHit2D hit = Physics2D.Linecast(worldPos, worldPos);
             
             if (null != hit && hit.collider.gameObject == gameObject)
             {
                 Animator anim = GetComponent<Animator>();
                 if (null != anim)
                 
                 {
                     Debug.Log("Playing Lopen");
                     anim.Play("Lopen");
                  }
                 
             }
             
         }
     }
 }
 

,

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 vintar · May 25, 2016 at 08:34 PM 0
Share

It will work fine but you need a reference to the puppet sum$$anonymous$$ like :

 Animator anim = Puppet.GetComponent<Animator>();
avatar image jonahsrocket vintar · May 27, 2016 at 08:20 PM 0
Share

Thank you for your answer. I was actually looking that direction. But when I add Puppet.GetComponent etc. the error reads 'The name Puppet does not exist in current context'.

Can't figure it out. $$anonymous$$aybe you can help me again.

Thank you! Cheers!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jonahsrocket · May 29, 2016 at 10:15 PM

Hi there,

I managed to solve my problem by using something completely different. I used this tutorial. Then I attached the animator of the animation that I wanted to run (the puppet) on the other game object. So if I click the other game object, the puppet will walk to the left. Just what I wanted. I am a happy guy!

Thanks anyway for answering!!!!!

Cheers!

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 PhantomSarcasm · May 25, 2016 at 09:53 PM

the problem is that with this code specifies these activating the animation of walking to the left ... I think. you would have to put another but changing the animation of walking by you to have to go right. or you can even use more switch to choose the animation given ... know how to use the switch.

     //this variable are placed above not within the functions
         public int valor;
         if(null!=anim)
         {
             //
             switch(valor)
             {
             case 1:
                 //the animation 1
                 animation.play("lopen");
                 break;
             case 2:
                 //the animation 2
                 //say the right but not like this your animator
                 // I do not know
                 //
                 anim.Play("right");
                 break;
             default:
                 break;
             }
         }


forgive my English use google translator...

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

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

96 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 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 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 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 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

Turn back using imported mixamo animation 0 Answers

Programmatically Ordering Game Objects in 2d / Hierarchy 0 Answers

Area Effector 2D and the Animator not working together. 0 Answers

How can I change the design of game_object at runtime? 0 Answers

Later grab doesn't jump 0 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