Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

When would OnCollisionEnter be thrown?

Discussion in 'Scripting' started by Sporech, Oct 25, 2015.

  1. Sporech

    Sporech

    Joined:
    Sep 14, 2015
    Posts:
    26
    Okay, first thing i want to clarify, is i know what OnCollisionEnter means. My question is, what kinds of physical collisions will it detect? If an object is bring translated every frame and it hits an object with a collider, it doesn't get thrown, whereas an object being propelled by a force added to its Rigidbody component does throw it...
    If you set location directly it seems to trigger it...
    Can i adapt my collision detection to change when it would be thrown?
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Setting the position directly shouldn't trigger the event or just not reliably.
    AddForce will always cause it to trigger.
    MovePosition will always cause it to trigger.

    Take a look at the docs, it might give some more insight into how it works.
    I don't think you can change how or when it would be thrown. You can change how objects interact with each other by setting up layers and adjusting the layer interaction in the physics setup.
     
  3. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    This should explain it a bit better (all the way at the bottom). At least one of them needs to be a rigidbody and none of them can be a trigger.

    This is because it's being "teleported" and the physics of that movement isn't being interpolated to track collision.
     
  4. Sporech

    Sporech

    Joined:
    Sep 14, 2015
    Posts:
    26
    Okay, i have two solutions in mind, but i don't know how to do them:
    1: I am already using translate because it works with an object's local axis, but is there a different way which will move based on local axis, and trigger colliders?
    2: I could just add force, i have some script which already does this, but it doesn't work (it just falls out of the barrel of the gun, and doesn't do anything). It doesn't work though, can anyone see anything wrong?
    Code (CSharp):
    1. proj.gameObject.GetComponent<Rigidbody> ().AddForce (new Vector3(guntarget.gameObject.transform.position.x - proj.transform.position.x,guntarget.gameObject.transform.position.y - proj.transform.position.y,guntarget.gameObject.transform.position.z - proj.transform.position.z));
    Guntarget is a transform just infront of the gun.
     
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    That AddForce is not adding very much force at all.

    Code (CSharp):
    1. Vector3 direction = guntarget.transform.position - proj.transform.position;
    2. direction.Nomalize();
    3. proj.gameObject.GetComponent<Rigidbody>().AddForce(direction * 250.0f);
    4.  
    I would also cache the Rigidbody component and not call GetComponent all the time.