Stopping Movement in Trigger Events?

Ok, I have a side scrolling character with a rigid body and a sphere collider set as a trigger. I then have walls and floors; some with a single box collider, some with compound box colliders (one box collider with 2 children box colliders), also set as triggers.

Now, the OnTriggerEnter events are working just fine (my player explodes when he hits them). My question is, in addition to this, how can I get the player to also stop moving when he hits one of these? I can’t use normal physics collisions because of all the other ways that the player interacts with objects in the game, so I need some kind of script solution to stop them from moving into one of these colliders.

Help? Thanks!

Stop all control signals that move the player and put his attached rigidbody to sleep.

That would stop them from moving entirely; including moving away from the wall. I only want to prevent them from moving through the wall.

You could always store your players last movement, and in OnTriggerEnter do the reverse.

This would keep him out, though he would not be able to slide over it. He would effectively be stuck until the direction changes.

Just wondering why you can’t use normal physics collisions? With Unity 3, they added in Physics layers (Edit->Project Settings->Physics) that allow you to select which layers can collide with what, and makes physics alot easier to use in complex games.

Other than that, do you basically just want a trigger collider to also act as a normal collider?

The entire game is essentially based on objects entering the trigger colliders of other objects and causing a reaction. Normal physics would get in the way. The player stopping when they hit the wall is really the only realistic physics that exist.

Seconding Dman. Store the last controller movement vector and just reverse it with some damping factor when hitting a wall.

Hello khanstruct,
We’re dealing with more or less the same problem, except that we’re moving characters around a sphere (sort of a Minigore on a sphere). Us too, we’re not using any physics because that would just make things more complicated. The downside is about colliding to obstacles around the sphere, so we guessed that stopping the movement and just bouncing the characters back could have been a good idea, but hed no idea on how to do it.
We got help from Myx here
We’re rotating, not moving, but apart from that, it stops the movement and rolls it back. We’re still testing it, because we’re not very good at coding yet, but I think our problem is similar so we’ll be following this thread…

Sorry, link missing on previous message

http://forum.unity3d.com/threads/79240-find-parent-object-s-collision

Hello people!

Oh neat, my name’s in the thread before I’ve posted.
Right then, here comes a piece of code which describes my thoughts on the problem. I assume this script is attatched to the player.

void OnTriggerEnter(Collider collision)
{
    if(collision.tag == "Solid")
    {
        Vector3 newPos = transform.position;
        newPos = collision.ClosestPointOnBounds(newPos);

        Vector3 collisionDir = transform.position - newPos;
        collisionDir.Normalize();
        collisionDir *= 1.1f;
        collisionDir.x *= collider.bounds.extents.x;
        collisionDir.y *= collider.bounds.extents.y;
        collisionDir.z *= collider.bounds.extents.z;

        transform.position = newPos + collisionDir;
    }
}

Okay, so basically what’s happening here is:
The player’s position is clamped to the other collider, in other words the player’s position is now at the point inside the other collider which is closest to the player’s position before the collision happened.
After that we calculate a direction from the new position and the old position. The direction this vector points will describe in which direction the player needs to be moved.
After getting the direction we multiply it by 1.1 to make sure we end up inside the other collider. After that the size of the player’s collider is multiplied with the direction.
Lastly we add the direction-change and the point within the other collider toghether to get the player’s new position.

Note that as with all code I write on the forums, this code is not tested so I have no clue if it will actually compile! If it does not, hopefully it’s still helpful in providing a general idea.

… that is not even close to my code.

	transform.Translate(Input.GetAxis("Horizontal")*speed,Input.GetAxis("Vertical")*speed,0,Space.World);

In essence, think of R-Type, and that’s the movement I’m going for. The player has a sphere collider, and the walls have box colliders on them. The walls will move slowly from right to left (to simulate movement).

Now, I’ve been messing with the code; adding and removing rigid bodies, etc. I can’t even seem to get basic physics collision to work (just don’t fly through the wall). So… my player doesn’t necessarily have to be a trigger, nor does it necessarily have to have a rigid body (though, then all my other objects would). What would be the simplest way to just stop this guy from flying through a wall.

…I think I’ve been staring at this too long.