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 Harry64 · Aug 05, 2013 at 12:27 AM · instantiatecolliderbouncephysicmaterialbounciness

Put PhysicMaterial on my Gameobject befor destroying it?

I have here a weird problem here... I have made a Cube that will when falling down on the floor instantiate a prefab ball that has a PhysicMaterial on it that is very bouncy (100% bouncy). the Cube has a PhysicMaterial that is not bouncy.

I have coded it so that the ball that gets instantiated gets the velocity and the angularVelocity from the cube. when the cube hits the floor with a specific relativeVelocity it will instantiate the ball and destroy itself.

but when the cube falls on the floor the ball spawns and dont bounce into the air...

when I throw the cube in a direction or so then the ball spawns and moves into the direction I have thrown it. so the script works!

I found out that when I put the same PhysicMaterial on the Cube like the Ball has then the Ball bounces into the air!!!

so I thought I could put the same PhysicMaterial on the Cube just befor it Destroys itself via script.... but I cannot get it to work. It just applys a default PhysicMaterial.

has someone any Idea how I could get the PhysicMaterial into the Cube from the prefab when it hits the floor.

or does someone know why my Ball wont bounce from the floor?

here is my Script.

 private var propInfo : Prop_Info;
 private var destroyMe : boolean = false;
 
 var canBreak : boolean = false;
 var brokenObject : Transform;
 var breakForce : float;
 
 function Awake()
 {
     propInfo = GetComponent(Prop_Info);
 }
 
 function Update()
 {
     if (destroyMe == true)
     {
         var object_to_destroy = this.gameObject;
         var instantiated_object = Instantiate (brokenObject, transform.position, transform.rotation);
         var vel = object_to_destroy.rigidbody.velocity;
         var ang_vel = object_to_destroy.rigidbody.angularVelocity;
         instantiated_object.rigidbody.velocity = vel;
         instantiated_object.rigidbody.angularVelocity = ang_vel;
         gameObject.collider.material = brokenObject.collider.material;
         Destroy(object_to_destroy);
     }
 }
 
 function OnCollisionEnter (collision : Collision)
 {
     if(canBreak == false)
     return;
     
     if (collision.relativeVelocity.magnitude > breakForce)
     {
         destroyMe = true;
     }
 }
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
2
Best Answer

Answer by Peter G · Aug 05, 2013 at 12:50 AM

Why don't you try this:

 #pragma strict
 
 private var destroyMe : boolean = false;
  
 var canBreak : boolean = false;
 var brokenObject : Transform;
 var breakForce : float;
 
 //It makes sense just to do all the work in the collision function.  Its just extra work to do the check in Update ().
 function OnCollisionEnter (collision : Collision)
 {
     if(canBreak == false)
     return;
  
     if (collision.relativeVelocity.magnitude > breakForce)
     {
        var object_to_destroy = gameObject;
        var instantiated_object = Instantiate (brokenObject, transform.position, transform.rotation);
        
        var vel = -Vector3.Reflect(collision.relativeVelocity, collision.contacts[0].normal);
        //This is the only line I really changed.
        //We take the incoming velocity, flip it over the normal of the contact, then flip the direction so it bounces back.
        
        var ang_vel = object_to_destroy.rigidbody.angularVelocity;
        instantiated_object.rigidbody.velocity = vel;
        instantiated_object.rigidbody.angularVelocity = ang_vel;
        Destroy(object_to_destroy);
     }
 }


What happens is that after your object collides with the ground, its velocity becomes very close to 0. Hence when you copy it over the end result is that the spawned object doesn't bounce any. Instead we can use the impact velocity to figure out how fast the object was going when it impacted, flip it, then send the debris going out at the same speed.

Comment
Add comment · Show 2 · 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 Harry64 · Aug 05, 2013 at 06:50 PM 0
Share

O$$anonymous$$G you are a genius!!!

it works perfect! but how. I thought now it would just spawn 4 prefabs like in my first code question but now it spawns just one ball?

can you tell me that please?

in my above code I wrote the Instantiate code into the Update function with the if destroyme == true thing because if I wrote all into the OnCollisionEnter function it would be like... the cube hits the floor with his flat surface and so the cube would get 4 collisionpoints. and then it spawned 4 prefab balls at the same point at the same time (frame). what is so different about this code then my code from my last question. here is my first try code:

klick me to get to my old question

if you could describe me what I did wrong in this code this would be Awesome! :-D

avatar image Peter G · Aug 06, 2013 at 01:40 AM 0
Share

Hmmmmm, I've never seen behavior like that so unfortunately I don't think that script was your problem though. But I'm glad it work now :)

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How are BounceCombine values combined? 1 Answer

Disable bouncing effect 0 Answers

How can I make different physic materials on one 3d object to make different frictions and bounciness? 1 Answer

character bounces on a box 0 Answers

Instantiate Prefab At Collided Object? 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