OnTrigger event and getting direction of collision.

Hiya,

I am using OnTrigger to track when a collision occurs. What I am hoping to do is to get the direction the collision is occurring from (left, right, forward, backward) and then move the object in the opposite direction.

Can anyone provide some insight on how I may be able to accomplish this with the collider that is returned?

Thanks for the help!

Regards,

– Clint

If the GameObject has a rigidbody attached, you could try getting the velocity of the rigidbody

function OnTriggerEnter(other : Collider)
{
  var othersVelocity : Vector3 = other.rigidbody.velocity;
  ... do stuff using the velocity vector ...
}

You would probably want some error checking though :slight_smile:

If the other object did not have a rigidbody you could have a script attached to the other object that kept track of the direction it was moving and reference that variable.

Thanks lfrog and I will give it a try!

In the game I am working on there are all these doors the user can open/close and move through… Man what a hassle I am having w/ the user getting stuck or the door just not closing. So one of the solutions I cam up with is if the door is closing and the user is in the doorway, that door will simply move the user out of the way.

I wonder how others handle something like this.

Regards,

– Clint

Cant take credit for this as I struggled for a while with math before I gave up and went out for some fresh air, but a friend came up with something that worked see script and package attached

basic rebound code (see attached package)

function Update() {
    transform.Translate(0, 0, 10 * Time.deltaTime);
}

function OnCollisionEnter(collision : Collision) {
	var contact = collision.contacts[0];
	transform.forward = Vector3.Reflect(transform.forward, contact.normal);
}

I am having problems trying to use the rebound with a fire though seems my fire is messing up the bounce

firing code

var projectile : Rigidbody; 
var speed = 20;	

function Update () { 
  if (Input.GetButtonDown ("Fire1")) { 
    
    var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
     
    instantiatedProjectile.velocity = transform.forward * speed;   
  }
  
}

dont know why …any suggestions welcome

-Update
worked out that I needed the function update even when I was firing the object as it requires a constant forward velocity.

30790–1127–$rebound_347.zip (6.06 KB)

Thanks adamprocter!

Gonna take a look and see if I can work it in.

Regards,

– Clint

I had this issues with doors as well. Your solution sounded like a good one, better than mine which was to disable the door collider until the player was no longer in the trigger area.