- Home /
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;
}
}
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.
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
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 :)