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 Vice_Versa · Apr 24, 2015 at 05:56 PM · collisiontimerendenumerate

Make a program wait for X seconds BUT Happen earlier on collision

I am working on a Whack a Mole type of game, im trying to get the moles to move up and down for random amounts of time (Which it does successfully) however, i want the moles to move back down if the mallot collides with the mole (the game notices the collisions and the moles bobble around, but they do not actually move back to the correct position until the time limit runs out. I am using an Enumerator that calls yield WaitForSeconds() in the start function, im guessing this is not the correct method for what i am trying to accomplish.

 using UnityEngine;
 using System.Collections;
 
 public class tester : MonoBehaviour {
 
 
 
     bool isMoving = true; 
     bool moleUp = false;
     bool isColliding = false;
     public AudioClip moleUpSound;
     public AudioClip moleDownSound;
 
     float X;
     float Y;
     float Z;
     // Use this for initialization
 
     void Start()
     {
 
         StartCoroutine ("moleMove");
     
                 
     }
         void Update()
         {
 
         if (this.gameObject.name == "Mole_1") {
             X = -1F;
             Y = 1.5F;
             Z = -1.6F;
         } else if (this.gameObject.name == "Mole_2") {
             X = 4F;
             Y = 1.5F;
             Z = -1.6F;
         } else if (this.gameObject.name == "Mole_3") {
             X = 9F;
             Y = 1.5F;
             Z = -1.6F;
         } else if (this.gameObject.name == "Mole_4") {
             X = -1F;
             Y = 1.5F;
             Z = -6.6F;
         } else if (this.gameObject.name == "Mole_5") {
             X = 4F;
             Y = 1.5F;
             Z = -6.6F;
         } else if (this.gameObject.name == "Mole_6") {
             X = 9F;
             Y = 1.5F;
             Z = -6.6F;
         }
 
 
 
         if (moleUp == false) {
             if (Y < 3F) {
 
                 moveUp();
 
             }
         }
 
         if (moleUp == true) {
 
             if (Y > 1F) {
         
                     moveDown();
 
 
             }
         }
     }
     IEnumerator moleMove ()
     {
         int i;
 
         for (i = 1; i > 0; i++) {
             if (moleUp) {
                 PlaySound ();
 
                 float timeLimit = Random.Range (2, 3);
 
                 moleUp = false;
                 yield return new WaitForSeconds(timeLimit);
             }
             else if (moleUp == false) {
                 PlaySound ();
 
                 float timeLimit = Random.Range (2, 6);
 
                 moleUp = true;
                 yield return new WaitForSeconds (timeLimit);
             }
         
             }
         }
     
     public void PlaySound()
     {
         if (moleUp) {
             audio.clip = moleUpSound;
 
             audio.Play ();
         }
         if (!moleUp) {
             audio.clip = moleDownSound;
             audio.Play ();
         }
 
 
     }
 
     public void OnCollisionEnter(Collision col)
     {
         Vector3 newPosition = new Vector3 (X, 3.5F, Z);
         Vector3 oldPosition = new Vector3 (X, -1F, Z);
         Debug.Log ("Collision!");
         isColliding = true;
         moleUp = false;
         iTween.MoveTo (gameObject, iTween.Hash ("Position", oldPosition, "time", 1));
         isColliding = false;
         //moveDown ();
     }
 
     public void moveUp()
     {
         if (isColliding == true) {
             isColliding = false;
         }
         Vector3 newPosition = new Vector3 (X, 3.5F, Z);
         iTween.MoveTo (gameObject, iTween.Hash ("Position", newPosition, "time", 1, "transition", "spring"));
         
     }
 
     public void moveDown()
     {
 
     
         Vector3 oldPosition = new Vector3 (X, -1F, Z);
         iTween.MoveTo (gameObject, iTween.Hash ("Position", oldPosition, "time", 1));
         isColliding = false;
     }
 }
 
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

1 Reply

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

Answer by siaran · Apr 24, 2015 at 07:22 PM

Not related to your question, but the first part of your Update loop makes no sense. You should just make a public Vector3 variable for the position and set it in the inspector for each object.

anyway, I would write this something like this, not using coroutines:

 //position
 public Vector3 startPosition;
 //last time this mole appeared
 float lastAppearTime;
 //last time we went down
 float lastHideTime;
 //time we want to be up
 float appearTime;
 //time to remain hidden
 float hideTime;
 //are we up?
 bool isUp;
 
 ///methods that move the mole up or down
 void Appear(){
  //appear 2-5 seconds. adjust as desired.
  appearTime = Random.Range(2,5);
  lastAppearTime = Time.time;
  isUp = true;
 }
 void Hide(){
  //hide for 2-5 seconds as well
  hideTime = Random.Range(2, 5);
  lastHideTime = Time.time;
  isUp = false;
 }
 
 void MoveUp(){
  //put some code here that moves object upward to desired position, I'm not writing it here
 //make sure that it stops when destination is reached. Vector3.MoveTowards does that autmoatically I think
 }
 void MoveDown(){
 //same as moveup except downwards.
 }
 
 void OnCollisionEnter(Collision col){
 //if we get hit with something, immediately hide.
  Hide();
 }
 
 
 void Update(){
  //if this object is showing itself
  if(isUp){
   //move upwards. MoveUp will contain a check to prevent it from going up too far.
   MoveUp();
    //if our appear time is up, hide ourself.
    if(Time.time-lastAppearTime >= appearTime){
     Hide();
   }
  }else {
   //same as moveup but in reverse
   MoveDown();
   //hide time is up, show self
   if(Time.time-lastHideTime >= hideTime){
    Appear();
   }
  }
 }

Well, I think that should do what you want, without coroutines.

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 Vice_Versa · May 04, 2015 at 07:42 AM 0
Share

This worked perfectly thank you

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The timer starts after a collision with an object 1 Answer

How To Reset Timer on a Collision (C#) 0 Answers

lightup on collision 2 Answers

I can't get a collision to cause the end of my game 1 Answer

Starting/Stopping a Timer On/Off Collision 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