How to freeze an object when collision?
Hello,
I have a 2D catch game, where you have to catch falling objects. Now I have a life and score script, when I don't catch my falling object, it collisions with the ground and changes the sprite. So I have bottles falling from the air, and when they fall on the ground, it changes the sprite to a broken bottle.
In this script, I have also set, if the (other.gameObject.GetComponent().sprite == broken) then use the function lifeSystem.TakeLife(); which will take a life off.
But my problem is, the bottle can rotate, so when the sprite has changed, it could rotate on the ground, so the game is seeing it as more collisions, so more lifes are taking off. Now what I want is when my bottle hits the ground and changes the sprite, I want it to stand still, so it can't rotate or move.
What also an option is: When my bottle hits the ground, it will paint a sprite on the position of where it fell. But I have no idea how to do this.
Bram
Answer by meat5000 · Oct 16, 2015 at 02:32 PM
If it has a rigidbody, make that rb Kinematic. If you dont want to make it kinematic manipulate the rigidbody 'Constraints'.
Yes I am using a rigidbody, but I can't turn on $$anonymous$$inematic, because then it won't fall down. And if I use the Contraints, I can't catch the bottle realistic, like if the bottle hits the edge of the catch object, it doesn't rotate.
No No, $$anonymous$$ake it $$anonymous$$inematic On Impact. This will freeze it in place after it has fallen. You can do this step in the OnCollisionEnter function.
If you want it to make a kinematic object move with another one, make the kinematic object a child of the other.
Okay I did this:
I made a public Rigidbody2D rb; (and put my bottles rigidbody into)
in the void start:
rb= GetComponent ();
and in the function of the impact:
rb.is$$anonymous$$inematic = true;
But when my bottle hits the ground, it isn't kinematic.
Answer by StianC · Oct 16, 2015 at 02:29 PM
If you are using a rigidbody on your falling objects, just turn it off on the collision. gameObject.GetComponent<RigidBody2D>().enabled = false;
If not, turn of whatever component is causing it to move down the screen.
Okay, that's sounds like the solution.
Here is my script:
public Sprite $$anonymous$$rolsch;
public Sprite $$anonymous$$apot;
private Life$$anonymous$$anager lifeSystem;
void Start () {
lifeSystem = FindObjectOfType<Life$$anonymous$$anager> ();
}
void OnCollisionEnter2D (Collision2D other) {
if (other.gameObject.GetComponent<SpriteRenderer>().sprite == $$anonymous$$rolsch)
{
other.gameObject.GetComponent<SpriteRenderer>().sprite = $$anonymous$$apot;
}
if (other.gameObject.GetComponent<SpriteRenderer>().sprite == $$anonymous$$apot)
{
lifeSystem.TakeLife();
}
}
How do I have to write this in the script?
Answer by BramNH · Oct 18, 2015 at 04:32 PM
Right now, when my bottle hits the ground and changes the sprite, i have set the following:
public Sprite Krolsch;
public Sprite Kapot;
public Rigidbody2D rigidbody;
private LifeManager lifeSystem;
void Start () {
lifeSystem = FindObjectOfType<LifeManager> ();
}
void OnCollisionEnter2D (Collision2D other) {
if (other.gameObject.GetComponent<SpriteRenderer>().sprite == Krolsch)
{
other.gameObject.GetComponent<SpriteRenderer>().sprite = Kapot;
}
if (other.gameObject.GetComponent<SpriteRenderer>().sprite == Kapot)
{
lifeSystem.TakeLife();
rigidbody.isKinematic = true;
}
}
So the sprite is changed, the rigidbody of it is kinematic.
But I am using a spawner, so when the next bottle spawns, it doesn't fall anymore, how to fix this??
Answer by constantlime830 · Jul 29, 2020 at 01:45 PM
@BramNH how about make a private bool called is smashed and set it to false, then go to the part of your script that changes it to kinematic and put is smashed there and set it to true then in an update function type
if(IsSmashed == true) { rb.isKinematic = false }
then the next one should fall
p.s i have no idea if this works or not, but i think it should :)
I was reading through my emails and saw someone had commented on me on the unity forum. I couldn't even remember I had used it :P This project has been left for dead a long time ago.
Still thanks for the answer! Even though I'm not gonna implement it :)
Your answer
Follow this Question
Related Questions
Sprites pixel colliding with 2d collider 0 Answers
Sprite Orbiting Sprite with Mouse 0 Answers
Freeze players position and rotation for a time? 0 Answers
How do I animate a 2D weapon? 0 Answers
shader graph sprite shader flickers in build when moving camera 0 Answers