I’m trying to create a simple ‘cover’ mechanic. I’m specifying which areas the user can hide behind with box colliders and using triggers to specify the players current state.
I am having problems figuring out how I would ensure the player can only move within the width of the box colliders bounds (so they can only shimmy against the wall).
Does anyone have any idea how I’d go about doing this?
Off the top of my head, I’d say you could probably clamp/convert the player’s movement/input based on the rotation of the bounding box. So within a certain angle threshold of player movement X, you’d just clamp the player’s movement to the bounding box’s local X.
Thanks for the reply. I’m struggling to understand the theory behind what you are saying… if you wouldn’t mind elaborating slightly that’d be really great
He is saying you need the wall’s angle, then use that angle to direct the player’s movement while they are in cover mode. If the wall is angled at 47 degrees and the player is in cover mode with that wall, then they should only move at 47 or 227 degrees until they exit cover mode.
Use OnCollisionEnter/Exit to trigger hidden/visible
I understand what you are asking for, you have an area dedicated as ‘cover’ for which you are using a box collider and you want it to be bounded like a Tom Clancey game.
Well in that situation then you want it so that OnCollisionExit triggers the movement script to say ‘stay put Mr’.
There are several methods to achieve this, for instance OnCollisionExit triggers something which makes the character move backwards two units(use -Transform.forward*2). You’ll have to trigger the OnCollisionExit in one script and in your movement script be listening for this event.
That is the easiest method I can think of, otherwise your character would get stuck, its easy yet its a little of a quick fix. I’m sure some games do this however.
Furthermore, you’ll want to make sure you can exit cover in the correct manner and that this script does not prevent that.
The clever method may be to use some of the following
For instance if you use bound.center and Bound.extents you could use another method.
You could say if player transform.position - bound.center > Bound.extents then ‘stop here Mr’!
Ahh, I see what you mean now by clamping. If I get the walls angle, I can use Mathf.Clamp to restrict the players movement relative to the wall. I suppose clamping JUST the x won’t work, because the wall might be at an angle… so instead I’d have to restrict the input axis.
That is some seriously complicated stuff… I’ll see if I can get any further with it and post some progress for anyone else who might want to do something similar.