Hi
Objects:
-Player (Box)
-Wall (Box)
Problem:
How to make the Player to be unable to enter the Wall?
I have tried:
[COLOR="red"]this script is attached on player
the "other" should be WALL[/COLOR]
void OnTriggerStay(Collider other)
{
if (transform.position.x > other.transform.position.x)
transform.Translate(new Vector3(1,0,0) * Time.deltaTime * 2 * Speed);
if (transform.position.x < other.transform.position.x)
transform.Translate(new Vector3(-1,0,0) * Time.deltaTime * 2 * Speed);
if (transform.position.z > other.transform.position.z)
transform.Translate(new Vector3(0,0,1) * Time.deltaTime * 2 * Speed);
if (transform.position.z < other.transform.position.z)
transform.Translate(new Vector3(0,0,-1) * Time.deltaTime * 2 * Speed);
}
it works but I’m not very happy with it because if I collide with wall and rotate player, the wall can “swallow” player, and then player can manipulate with rotation and moving (inside of wall) to get to the other side of the wall. And I don’t want Player to be able to go through the walls…
What I want:
How does the expert programmer solve this simple problem?