The Question About Colliding, C#

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?

I don’t think I’m understanding you because Unity should handle all this for you. Planes and Character Controllers have colliders attached to them by default, you shouldn’t need to code this manually.

wow! really? can you explain me a little bit closer how can I turn on that function?

I’m not sure how best to explain this. Try having a read through this…

wow! now I did it,nice
first I made a football (using rigidbody and collider) so my player could move it around
but then I set rigidbody on the player so that ball rejected player every time when player bumped into the ball

ty, this power of rigidbody is really awesome!