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 icelander · Dec 02, 2011 at 02:59 PM · collisionspawnmoving

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

Comment
Add comment · Show 2
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 Kacer · Dec 02, 2011 at 05:56 PM 0
Share

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.

avatar image Kacer · Dec 06, 2011 at 11:18 AM 0
Share

When you're answering other people answers, please do it as a comment, otherwise you'll just clutter the thread

5 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · 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
1

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

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 stream-markus · Jun 28, 2016 at 04:54 AM 0
Share

Thnx For THis mate Can you please tell how to move the object in four different Direction randomly .. ,By this script

avatar image
0

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

Comment
Add comment · 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
0

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

Comment
Add comment · 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
0

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.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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