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 Tageos · Jun 25, 2015 at 04:00 PM · c#pause

Pause movement on collision

Hello again!

I have made a script which should stop the movement of an enemy when it touches the player. When the enemy stops moving it charges an attack and on the 3rd second he attacks. (If the player is still within the enemy collider he takes damage). After the attack which should take 3 seconds, he continues moving towards the player.

I have made a script which makes the enemy move torwards the player and stop his movement when he the collider is triggered. My problem is that he does not continue walking towards the player (after the 3 seconds). Could you perhaps help me to modify my script so that the enemy after the 3s pause continues moving towards the player?

The code: using UnityEngine; using System.Collections;

 public class ZombieMovement : MonoBehaviour {
 
     public Transform player;
     public float moveSpeed;
     public float AttackAnimDuration = 3f;
     private bool paused;
 
     IEnumerator delay()
     {
         paused = true;
         yield return new WaitForSeconds(AttackAnimDuration);
         paused = false;
     }
         
         void Update () {
 
         if (paused)
         {
             return;
         }
     
         float move = moveSpeed * Time.deltaTime;
         transform.position = Vector3.MoveTowards (transform.position, player.position, move);
 
     }
     void OnTriggerEnter2D(Collider2D col)
     {
          
         if (col.gameObject.tag == "Player") 
 
         {
             paused = true;
         
         }
     
     
     }
     
 }

Thanks in advance :)

/Taegos

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by kmgr · Jun 25, 2015 at 06:31 PM

You're not calling the delay() coroutine and because of that after the collision your paused variable remains false.

 void OnTriggerEnter2D(Collider2D col)
 {
     if (col.gameObject.tag == "Player")
     {
         StartCoroutine(delay());
     }
 }


This should work as you described.

Comment
Add comment · Show 2 · 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 Tageos · Jun 25, 2015 at 07:44 PM 0
Share

Thank you so much! This worked.

avatar image Tageos · Jun 25, 2015 at 10:36 PM 0
Share

What would i modify in this code if i wanted to make the enemy repeat this delay if the player is still triggering the collider (standing still)?

Now, if i walk into the colider the enemy movement pauses for 3 seconds and then it keeps moving into the player, without pausing anymore. The enemy only pauses when i stop triggering its collider and retriggers it (walking out of the box collider and re-enters).

Basicly what i want to do is:

if player is triggering collider

pause enemy movement for 3s (enemy attack animation)

if player is still triggering collider (the player is standing still)

pause enemy movement for 3s (enemy attack animation)

if player is STILL triggering collider (the player is STILL standing still)

pause enemy movement for 3s (enemy attack animation)

and so on...

I guess i would check in the update if the enemy collider is triggered somehow.

avatar image
1

Answer by Wolfdog · Jun 25, 2015 at 05:37 PM

 paused = true;
 yield return new WaitForSeconds(AttackAnimDuration); // you return here, so you never change paused to false.
 paused = false;

Your problem is that pause never gets set to false.

I would do it like this:

 using UnityEngine; using System.Collections;
 public class ZombieMovement : MonoBehaviour {
     public Transform player;
     public float moveSpeed;
     public float AttackAnimDuration = 3f;
     private bool paused = false;
  
     private attackTimer = 0;
          
         void Update () {
  
             if (!paused){
                 float move = moveSpeed * Time.deltaTime;
                 transform.position = Vector3.MoveTowards (transform.position, player.position, move);
             } else {
                 attackTimer += Time.deltaTime;
                 if (attackTimer >= AttackAnimDuration) {
                     attackTimer = 0;
                     paused = false;
                 }
             }
         }
     void OnTriggerEnter2D(Collider2D col)
     {
           
         if (col.gameObject.tag == "Player") 
  
         {
             paused = true;
          
         }
     }
 }

(untested - let me know if there are any problems)

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 Tageos · Jun 25, 2015 at 07:47 PM 0
Share

The first answer solved my problem using the IEnumerator function which i was using also. I havent tried this but i appretiate your comment, it looks like it would work.

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Does the application have to suspend? 2 Answers

Make enemy wait before attacking player 2 Answers

Pause Menu problem 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