Olivier Pons
Olivier Pons

Reputation: 15816

Elements still bouncing

update - Added a video

I've made things like you can see in many tutorials and forums:

Now I add another cube for the ground, made it very large and added a Box collider with Material = "No Bounce"

I have 2 problems: - when they collide, the cube bounces (whereas it shouldn't with my configuration) - I've made a script and attached it to the cube, to change the velocity, and set it to 0 when there's a collision:

using UnityEngine;

public class CubeProperties : MonoBehaviour
{
    private Rigidbody _rb;
    private bool _landing;

    private void Start()
    {
        _rb = GetComponentInParent<Rigidbody>();
    }

    public void OnCollisionEnter(Collision collision)
    {
        Debug.Log("Collision");
        _landing = true;
    }

    public void FixedUpdate()
    {
        if (!_landing) {
            return;
        }
        _rb.velocity = Vector3.zero;
        _landing = false;
    }    
}

So at the first collision, I try to instant stop the cube with _rb.velocity = Vector3.zero;. But changing velocity has no effect, I dont understand why. I've tried with many value to see what happens... but nothing happened. The only thing I can add, and it's working, is: AddForce() I tried to a negative value, but this doesn't work either.

What did I forgot?

Here's a video I hope this is easy to understand (and I hope I'm allowed to help with a video):

https://youtu.be/I3C1KBmm5yw

Upvotes: 2

Views: 399

Answers (1)

Tyler C
Tyler C

Reputation: 633

Looks like your mixing 2D physics and 3D physics together. If it's a 2D scene, you'll actually want to use the 2D Rigidbody and Box Collider 2D.

If it's a 3D Scene which is what it seems like, then you just want to make sure you're using the normal OnCollisionEnter. As it stands, the OnCollisionEnter2D won't get called in that setup.

Just to help see if things are getting called, a good tip in Unity is the Debug.Log. It'll send a message to the console if it gets fired off.

Upvotes: 3

Related Questions