Marc Rasmussen
Marc Rasmussen

Reputation: 20565

Object doesnt collide

I have the following script:

using UnityEngine;
using System.Collections;

public class BallScript : MonoBehaviour {
    public float speed;
    private Rigidbody body;
    // Use this for initialization
    void Start () {
        body = GetComponent<Rigidbody> ();
    }

    // Update is called once per frame
    void Update () {
        body.AddForce(transform.forward * speed);
    }

    void OnCollisionEnter(Collision collision) {
        Debug.Log("Collision");
        Destroy (this);
    }
}

AS you can see this has an OnCollisionEnter however my object is unable to Collide with anything instead it flys through every object?

This is my prefab:

enter image description here

And the object im trying to collide with:

enter image description here

Can anyone tell me what ive done wrong?

Upvotes: 0

Views: 67

Answers (1)

David Espino
David Espino

Reputation: 2187

If you want to use the isTrigger you need to check the collision with OnTriggerEnter. If you want to use the OnCollisionEntertry adding a rigidBody to the cube with the is kinematic (for performance) flag, disable the is trigger from the sphere and see if that works.

Check the difference of the methods here:

https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

Regards

Upvotes: 2

Related Questions