- Home /
right answer was accepted
how to stop player movemnt when there is something near him
im making a crossy road style game and i want that when the player is near a wall i dont want him to move anymore in that direction
so how can i do it without raycasting because sometimes it didnt detect it
or if there is a solution to define the width of the raycasts ?
Answer by hassanyawar · Jun 23, 2017 at 11:12 AM
Add a trigger collider in front of player. Give the wall a specific tag like 'Wall'. Use something like this to check:
void OnTriggerEnter (Collider obj)
{
if (obj.CompareTag("Wall"))
{
// stop the player
}
}
Answer by shadowpuppet · Jun 12, 2017 at 11:21 PM
without raycasting.....you could put trigger zones close to the wall so when player enters he gets a new destination on his navmesh. agent.SetDestination( some object). but ..if he is user controlled isn't up to the player to determine to not go in that direction ( of the wall)
thanks for the answer if you can explain in more details to know how to do it exactly step by step
Answer by IMemeManI · Jun 23, 2017 at 09:11 AM
I'm more of a 3D developer but my guess would be to detect a collision and stop the Player moving forward? Make the object have a collider and the Player script use OnCollisionStay
to detect the collision, when it's detected, make the movement forward stop by simply making a bool:
bool canWalk;
void OnCollisionStay(Collision Coll)
{
if(Coll.gameobject.tag == "wall")
{
canWalk = false;
} else
{
canWalk = true;
}
}
Apply "canWalk" to your movement script so when it detects the input "W" or if it's automated, in the "if" statement make sure you include, "&& canWalk", as if "canWalk" is false, the forward movement will not execute which in turn, stops the Player moving forward as long as the object he collides with has the tag "wall", or whatever you wanna call it.