- Home /
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;
}
}
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.
Your answer
Follow this Question
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