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 /
avatar image
0
Question by Ejpj123 · Apr 18, 2016 at 04:59 AM · animationplaymovingstopcollsion

Play Animation and Stop Moving on Enemy Collision

Hi everyone, I am making a 2d Infinite Runner, and I am having a struggle to figure out how to play an animation AND stop moving on enemy collision. Here is my script:

 using System;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 namespace UnityStandardAssets._2D
 {
     public class Restarter : MonoBehaviour
     {
         
         void OnCollisionEnter2D(Collision2D coll)
         {
             if (coll.gameObject.tag == "Enemy") {
                 GetComponent<Animation>().Play("RedGiantDEATHTOALL");
 
             }
                             
             
         }
     }
 }
 


Thank you for your help!!

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 Ali-hatem · Apr 18, 2016 at 11:47 AM 0
Share
  float $$anonymous$$oveSpeed = 5f;
      void OnCollisionEnter2D(Collision2D coll)
      {
          if (coll.gameObject.tag == "Enemy") {
              $$anonymous$$oveSpeed = 0f;
              GetComponent<Animation>().Play("RedGiantDEATHTOALL");
          }  
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SolAZDev · Apr 18, 2016 at 07:14 AM

The animation seems alright. But to have the movement stop, I'd have a bool and a coroutine, the Bool will allow the player to move, while the coroutine holds the bool to until the animation plays. Something like this:

 bool CanMove = true;
 bool MoveWait=false;
 
 void OnCollissionEnter2D(Collision col1){
  if(col1.gameObject.tag=="Enemy"){
     GetComponent<Animation>().Play("RedGiantDEATHTOALL");
     CanMove=false;
    if(!MoveWait)
       StartCoRoutine(WaitToMove());
  }
 
 IEnumerator MoveToWait(){
  MoveWait=true;
  return yield new WaitForSecconds(1.5f); //Change to Animation Duration
  MoveWait=false;
  CanMove=true;
 }
 

Of course, you'll have to return the MoveDirections to zero, or just check if CanMove is true while checking for keypress.

Comment
Add comment · Show 11 · 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 Ali-hatem · Apr 18, 2016 at 11:54 AM 0
Share

i will go with $$anonymous$$oveDirections to zero

avatar image Ejpj123 · Apr 18, 2016 at 01:13 PM 0
Share

this is what I got: alt text

screenshot-2016-04-18-080905.png (85.8 kB)
avatar image Ali-hatem Ejpj123 · Apr 18, 2016 at 01:58 PM 0
Share

you missed to ad } after StartCoRoutine(WaitTo$$anonymous$$ove());

avatar image SolAZDev · Apr 18, 2016 at 03:13 PM 0
Share

Sorry, I didn't write it on a Text Editor so I was prone to having an error or two. Try making the IEnumarator public, and ins$$anonymous$$d of return yield new, it's yield return new.

You are missing a } closing bracket for the Collission Checking.

avatar image Ejpj123 · May 05, 2016 at 01:34 AM 0
Share

I made the IEnumerator public but this happened.

alt text

screenshot-2016-05-04-203047.png (86.2 kB)
avatar image Ali-hatem Ejpj123 · May 05, 2016 at 10:40 AM 0
Share

read the error it tells you the problem : add using System.Collections; to the top of the script & StartCoRoutine ($$anonymous$$oveToWait()); not WaitTo$$anonymous$$ove

avatar image Ejpj123 · May 07, 2016 at 05:36 PM 0
Share

For some reason more errors pop up when I fix the errors that show:

alt text

screenshot-2016-05-07-123403.png (106.4 kB)
avatar image Ali-hatem Ejpj123 · May 07, 2016 at 06:17 PM 0
Share

thees tow errors means you have miss typed some thing you can google any thing about unity to open unity documentation to cheek what you missed . & stop posting images just copy/pest your code with the error log . lastly for now here is the errors :

StartCoroutinenot uper R case for routine & WaitForSeconds not double c for Seconds . see the doc : StartCoroutine

avatar image Ejpj123 Ali-hatem · May 07, 2016 at 06:36 PM 0
Share

Thank you, and I will take your advice and put it in action.

Show more comments

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

78 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

Related Questions

Walking Animation 1 Answer

Play a video on clicking on an object 1 Answer

Automatically playing animations 1 Answer

Animation problem in script 0 Answers

Playing Animation When Two Keys Are Held Down To Play Run Animations 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