Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 superlol · Jul 31, 2011 at 09:47 PM · instantiatecolliders

Create a new object after the collision of two others

Hi folks!

I need you opinion about this :)

please see linked image

alt text

I need to create a new object from the collision of two identical objects, then make the size of the new one equal to the sum of the two objects sizes.

Any suggestions?

Tnx :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Chris D · Jul 31, 2011 at 10:14 PM

What you need to look at are:

  1. questions on instantiate
  2. questions about collisions
  3. the documentation on instantiate and collisions

Getting the sizes right is a matter of calculating their volumes and adding them together. If they're primitive type objects, it'll be easy enough; if not, it could get a little trickier (or just guess-timate it).


Edit: question is actually a little trickier than just destroy and instantiate. This code successfully detects collisions between two rigidbodies, destroys them, and instantiates only one larger object:

var spherePrefab : GameObject; var willSpawn : boolean = true;

//set a flag depending on if there's already been a collision or not function canSpawn(b: boolean){ willSpawn = b; }

function OnCollisionEnter(other:Collision){ print("collision"); var othGO: GameObject = other.gameObject;

 //check the flag first so you don't do an unnecessary lookup
 if (willSpawn && (othGO.name == name || othGO.name == name + "(Clone)")){ //assuming you're looking to compare two objects with the same names
     othGO.SendMessage("canSpawn", false);
     var newSphere: GameObject = Instantiate (spherePrefab, other.contacts[0].point, Quaternion.identity);
     newSphere.transform.localScale += Vector3(2,2,2);
 }
 Destroy(gameObject);

}

Comment
Add comment · Show 3 · 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 superlol · Jul 31, 2011 at 11:00 PM 0
Share

Hi Chris thanks for the answer!

I just know some basics about istance anc colliders and my question was more about the initial approach.

For example it's better to have a listener outside objects that create a new one or just a script inside them?

I already check collision between the two objects and I make an instance if the collision is with an object with the same name and destroy the original...the problem is that the script is the same in each object so I'm getting...two new instances! :D

avatar image Chris D · Jul 31, 2011 at 11:53 PM 0
Share

Ah, I see what you're after now. In that case it's not as straight forward, but still not bad. See my edited answer above.

The only issue I've run into with this code is that it starts running into issues with successive generations of instantiations. I added the alternate case of running into a clone (the third check in the if statement) and that takes care of the second generation. It should be good enough to show you the general approach, though :D

avatar image Blurpo · Aug 08, 2013 at 10:22 AM 0
Share

Hey, Im sorry to bother you guys, especially on such an old post but I just wanted to ask if there was a way to me the sphereprefab move ins$$anonymous$$d of staying still when Instantiated. I would like there to add some force to it from the collision of the 2 smaller objects. I tried to have the variables as Rigidbodies but then one of the smaller objects doesnt get destroyed. I have a Rigidbody attached to both my sphereprefab and the smaller objects. Heres a sample of my slightly modified code from above : var EnergyBall : GameObject; var willSpawn : boolean = true; function canSpawn(b: boolean){ willSpawn = b; } function OnCollisionEnter(other:Collision){ var othGO: GameObject = other.gameObject; if (willSpawn && (othGO.tag == "Bullet" || othGO.tag == "Bullet" + "(Clone)")){ othGO.Send$$anonymous$$essage("canSpawn", false); var NewBall: GameObject = Instantiate (EnergyBall, transform.position, Quaternion.identity); } if (other.collider.tag == "Bullet") Destroy(gameObject); } I cant get the "EnergyBall" to have any kind of force. I would use add force but that would have to be in a specific direction. I want to use the force of the two smaller objects to have an influence on how fast the EnergyBall goes and which direction it goes. Thanks for reading all this, heh. Its not really a comment but whatever, I guess. Sorry to bother you guys and have a nice day.

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

Invisable Projectile; Instantiate at Collisions 2 Answers

Checking if object intersects? 1 Answer

Can anyone help check for colliders in this spawning script so that the spawns don't overlap each other or spawn inside of walls, objetcs etc 0 Answers

A tricky problem with Particle Collisions 0 Answers

Instantiated object not colliding when not have a rigidbody?? 2 Answers


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