- Home /
Whack-A-Mole Issue
I'm in the process of making a whack a mole minigame and came across an issue: I can get all of the "Moles" (cylinder game objects made in Unity at the moment) to raise up to the proper height, but they all move at the same time. I need to make them move randomly up to that height and wait for a set amount of time before going back to their starting position. I already have the code for clicking the moles, detecting the hit, and adding to the score, I'm just having an issue making them move randomly and independent of each other.
var pointB : Transform;
private var pointA : Vector3;
var speed = Random.Range(.5, 3);
var offset = Random.value * 123;
function Start () {
pointA = transform.position;
}
function Update()
{
var i = Mathf.PingPong(Time.time * speed, offset);
//transform.position = Vector3.MoveUp(transform.position,OriginalTransform,Speed);
if(transform.position == pointB.position)
{
//yield WaitForSeconds (5);
print("We are here");
}
transform.position = Vector3.Lerp(pointA, pointB.position, i);
}
//waitforseconds
//random
Here is the working code I have handling the movement of the moles:
var pointB : Transform; private var pointA : Vector3; var speed = 1.0;
function Start () {
pointA = transform.position;
}
function Update() {
var i = $$anonymous$$athf.PingPong(Time.time * speed, 1);
if(transform.position == pointB.position)
{
print("We are here");
}
transform.position = Vector3.Lerp(pointA, pointB.position, i);
}
Oops I accidently deleted the first comment.
@Andrew: please put the code in your question and use the 010101 button to use code formatting so that it is readable.
Sorry about that. I just edited the post, and also updated it with the suggestion from Warwick and a couple of the other things I attempted.
Answer by Waz · Jul 29, 2011 at 05:14 AM
In Start do:
offset = Random.value * 123;
then add this var to the value passed to PingPong.
That's really close. Please forgive me if this seems like a dumb question, but would changing the 123 lower the amount of time between movement? They all go to the top at different speeds, but they stay there for way too long. Thank you for the help, much appreciated!
Your answer

Follow this Question
Related Questions
Simple array an spawning question 6 Answers
Make an object move towards random spot on another objects edge? 1 Answer
Puzzle + Grid Instantiate - Random 2 Answers
Make object move in random directions 1 Answer
InvokeRepeating with a variable? 2 Answers