- Home /
How to make realistic collision?
Hey dear experts,
I have an issue with my Scene. Basically what I want is, when the Knife and the white Barrier collide, the Knife should spin around and fall. However, currently my Knife just stop moving after the collision (because Gravityscale is 0).
I provide an image of the scene: https://i.imgur.com/t4j4qAf.png
Thanks in advance! :-)
Answer by Ejpj123 · Oct 12, 2018 at 07:01 PM
Just set the gravity scale to 1 when the knife collides with the white barrier:
using UnityEngine;
public class Knife : MonoBehaviour {
Rigidbody2D rigid;
// Use this for initialization
void Start () {
rigid = gameObject.GetComponent<Rigidbody2D>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.name == "WhiteBarrier")
rigid.gravityScale = 1;
}
}
Also make sure there there is a Box Collider 2D attached to both the knife and the white barrier.
Your answer
Follow this Question
Related Questions
increasing knockback of a rigidbody as received damage increases (like super smash) 1 Answer
Stack of RigidBody2D, Freeze X and Z, Odd Behavior 1 Answer
Rigidbody2D: Follow other Rigidbody2D strictly 1 Answer
Swinging a bat to hit a falling ball 0 Answers
Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer