- Home /
freeze an object for a while or reset value or reverse it?
Basically I have a red sphere that chases around green spheres. The green spheres wander around by themselves by going from randomly generated way point to randomly generated way point. Everything is working great, however no matter what I do I cannot make them continue running around after they collide with something. Instead the rigidbody spheres break their movement and fly in one direction until they hit a wall. I want to be able to send my green spheres in the opposite directions after a collision (so if their heading to (2, 1, 3) now they'd go to (-2, 1, -3) or have then choose a new way point or something. I'll post up my script any help would be appreciated. The script below just manages health and the wandering.I tried reusing the Wander function that you will see in the script for the collisions but it makes them extremely jittery and they just spaz out.
var health : int= 30; var dead : boolean= false; var DeadSpeed : float= 0.5; var Speed= 10; var wayPoint : Vector3;
function Start() { Wander(); }
function OnCollisionEnter(collision : Collision) { if(!dead && collision.gameObject.tag=="P Bullet") { health -= 10; Debug.Log(health); if ( health <= 0) { dead = true; } }
if(collision.gameObject)
{
//Make the object stop? Go in the opposite direction? or choose a new waypoint???
}
if(collision.gameObject.tag=="Player")
{
//Make the object stop? Go in the opposite direction? or choose a new waypoint???
}
}
function Update() { var dist : float =(Appear.ActivePos - transform.position).sqrMagnitude; transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
if((transform.position - wayPoint).magnitude < 3 && dead != true)
{
Wander();
}
if(dead)
{
if(dist >= 2)
{
transform.LookAt(Appear.ActivePos);
transform.Translate(Vector3.forward * DeadSpeed, Space.Self);
}
else
{
transform.position=Appear.ActivePos;
Destroy (gameObject);
Appear.ActiveObj.active= true;
Appear.visible.Add(Appear.ActiveObj);
}
}
}
function Wander() { wayPoint= Random.insideUnitSphere *120; wayPoint.y = 1; transform.LookAt(wayPoint); }
Answer by Panamamigo · Jul 25, 2010 at 06:53 PM
After working on this problem for hours more I have discovered the problem. Some of the spheres would collide with the ground and since that was a collision with a gameObject it would keep changing waypoints. I fixed it using tags and now it works fine.
Your answer
Follow this Question
Related Questions
Object going through another....How to solve it? 0 Answers
How to prevent movement in a specific direction only? -1 Answers
Simple movement problem. Need units to stop dead on collision. 3 Answers
Mouse Look Freeze on Pause? 1 Answer
Unreliability of Physics.Raycast.Distance for player movement? 0 Answers