- Home /
How to change physic material during game when the player hits an object?
I'd like to change bounciness in Physics material with a script. So when player hits gameobject bouncines changes for some %. Until now I've made a script that doesn't work. The bounciness stays the same when player collides with game object. Also my script would only change bounciness to 0, so if anyone has an idea how to change bounciness for few % every time player hits this object please do not hesitate to inform me :) Here is the script: (I'm a self-taught beginner so please be understanding :) )
var PlayerColl: Collider;
var material: PhysicMaterial;
var dynamicFriction=2;
var target:Transform;
var col : Collider;
function Start() {
PlayerColl = GetComponent.<Collider>();
material = new PhysicMaterial();
material.dynamicFriction = dynamicFriction;
material.bounciness = 1;
PlayerColl.material = material;
}
function OnTriggerEnter(col : Collider)
{
if(col.transform.name == "Player")
{
material.bounciness = 0;
}
}
i also tried with this but still no effect..
function OnTriggerEnter (col : Collider){
if (col.transform.name == ("Player")){
PlayerColl = GetComponent.<Collider>();
material = PhysicMaterial();
material.dynamicFriction = dynamicFriction;
material.bounciness = 0;
PlayerColl.material = material;
}
}
Thank you for your time..
Answer by FelixTwoMenAndADog · Jun 30, 2016 at 02:32 PM
You can also change the material values directly, but keep in mind that they will change for every object using them.
Though I encountered a bug where you have to disable and enable the collider for the new values to take effect.
Thank you for mentioning this bug. I added this (disable an enable collider) to my code that changes a physics material property, and it went right to work! I spent 2 hours trying everything yesterday. Thank you!
Answer by GioVil · Feb 16, 2016 at 04:26 PM
Have you tried to set the bounciness in the component directly without to create a new material?
Do you mean if I tried to create material in game and than do a script that changes this material in game? Yes I did but it doesn't work..the same as with my example..bounciness doesn't change..
@GioVil I tried it again..to make a material in game and add it to the olayer collider and than try to change it with a script. So now my script looks like this..but the script wants to change material to object that the player colides with and not the player's.
$$anonymous$$issingComponentException: There is no 'SphereCollider' attached to the "DecreseBouncines" game object, but a script is trying to access it. You probably need to add a SphereCollider to the game object "DecreseBouncines". Or your script needs to check if the component is attached before using it. 2ball.Update () (at Assets/2ball.js:15)
var PlayerColl: SphereCollider;
var material: Physic$$anonymous$$aterial;
var target:Transform;
var col : Collider;
function OnTriggerEnter(col : Collider)
{
if(col.transform.name == "Player")
{
PlayerColl = GetComponent.<SphereCollider>();
material = Physic$$anonymous$$aterial();
material.dynamicFriction = 2;
material.bounciness = 0;
PlayerColl.material = material;
}
}
@martipello If the script is attached to the player or to empty object, the collision is not detected.
Answer by nogotogon · Nov 26, 2018 at 12:26 PM
I am trying to write something similar, and Am getting it to change, but the changes only go into affect when the scene reloads. Do you have any idea how to have the game update / reload an object or material without resetting the scene?
Your answer
