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 ?
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)
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.
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
}
}