- Home /
Respawn a moving object after collision
Hi everybody I would like to say first that this community is amazing !
I am quite new in unity3d and not very good in scripting and I have a problem with collision and respawn. Here is the thing : I have two balls A and B. BallA is moving towards ballB(fixed). when ballA and ballB collide I would like the ballA to be spawn at its first position and keep on moving towards ballB position in a forever loop : move, collide, spawn, move, collide, spawn... I have three object ball1 with rigbody attribute and a "move to point b script" attached to it, ball2 with sphere collider and respawn script attached, ball1pivot used as the initial respawn position.
Here are the codes that I am using : Move to point B code with speed control attached to ball1
var pointB : Transform;
private var pointA :Vector3;
var speed = 1.0;
function Start () {
pointA = transform.position;
while (true) {
var i = Time.time * speed;
transform.position = Vector3.Lerp(pointA, pointB.position, i);
yield;
}
} and the script for respawn attached to ball2
var spawn : Transform;
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.name == "ball1")
{
Debug.Log("Hit the ball");
theCollision.transform.position = spawn.position;
}
}
The balls are colliding, collision is detected but ball1 doesn't spawn at its first position. I tried to put all this in one script attached on ball1 and changing the collide object´s name ball1 by ball2 it worked but it was ball2 that was spawning and not ball1 the prob is that I was not able to get ball1 to relocate at its first position I so bad in script I need some help ! regards Icelander
Welcome to the Unity3D community :)
As a new guy please remember to always format your code properly, and to mark a question as answered when a proper answer has been posted.
enjoy your stay.
When you're answering other people answers, please do it as a comment, otherwise you'll just clutter the thread
Answer by icelander · Dec 04, 2011 at 09:58 PM
Hi again, I tried to post yesterday but I was not published ( could it be because I had trouble formatting my code? I apologize for that). any way I tried your method ( I think :s) put my both scripts in one attach to ball1 and in the respawn function I call the function start() within the condition.The collision is detected but ball1 doesn't respawn I even made a var to select the ball1 for the respawning object but ball1 doesn't spawn at its initial position I tried with other object and they spawn correctly here is my script
var spawn : Transform;
var pointB : Transform;
private var pointA : Vector3;
var speed = 1.0;
var ball1 : GameObject;
function Start () {
pointA = transform.position;
while (true) {
var i = Time.time * speed;
transform.position = Vector3.Lerp(pointA, pointB.position, i);
yield;
}
}
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.name == "ball2")
{
Debug.Log("Hit the ball1");
ball1.transform.position = spawn.position;
Start() ;
}
}
I really need some help with that
regards
Icelander
Answer by icelander · Dec 04, 2011 at 09:57 PM
I found a way to fix it. In fact I didn't need to detect collision, position data were enough. I found a script that move object from A to B to A ( thanks to Eric5h5 ) I got rid of the move back part and then just added more speed control. I think that your approach was also good Kacer but my inexperience in scripting made it too difficult for me to fix
I leave here the modified script speed control from 0.0 to infinite
var pointB : Vector3;
var rate = 1.0;
function Start () {
var pointA = transform.position;
while (true) {
yield MoveObject(transform, pointA, pointB, 3.0);
}
}
function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float) {
var i = 0.0;
while (i < 1.0) {
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield;
}
}
Thanks a lot everybody for the help!
regards
Icelander
Thnx For THis mate Can you please tell how to move the object in four different Direction randomly .. ,By this script
Answer by Kacer · Dec 02, 2011 at 06:03 PM
I think your problem lies in the Start method, that method is only run when the script is first enabled, in this case, when you start the game.
So when movement is over and the collision has happened, then nothing else is being done.
So, to make this work, i would put the lines of code into a method of its own lets call it "MoveMe()", and when the collision is detected you then call the method.
judging from your script, if you do that, then it should not be neccessary to call the "transform.position" in the OnCollisionEnter Method.
Though, i'm not entire sure this works, it does in my head, thing is, unity does not always work as my head wants it to do though :P
Answer by icelander · Dec 03, 2011 at 01:41 AM
Thanks a lot for answering
I think I understand what you mean. I will try soon as I come home
thanks again
Answer by icelander · Dec 05, 2011 at 01:05 AM
Hi I'm back. Well, I tried your idea and I may have not done it correctly and it doesn't work. to make it simple I have regrouped both script in one attach to ball1. the balls collide (collision detected) but ball1 doesn't respawn at its first position. ball1 just keep on spinning at the collision point. It seems that ball1 want to continue moving but won't relocate at its first position, I don't know how to fix this. :( if someone could show me some script example here is my new script
var spawn : Transform;
var pointB : Transform;
private var pointA : Vector3;
var speed = 1.0;
function Start () {
pointA = transform.position;
while (true) {
var i = Time.time * speed;
transform.position = Vector3.Lerp(pointA, pointB.position, i);
yield;
}
}
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.name == "ball2")
{
Debug.Log("Hit the ball");
Start() ;
}
}
I am a bit lost, could it be that the initial position of ball1 is not reseted when the collision happen in the function Start() :( how could I fix this? regards.
Ps I have some difficulty to format my code sorry if it come up messed up.
Your answer
Follow this Question
Related Questions
spawning on collision 1 Answer
Respawn random objects 1 Answer
Making the object collide while moving in the forward dir. (Vector3.forward) 1 Answer
Spawning if there is space 1 Answer
Moving Randomly 1 Answer