- Home /
Collision between Player and a Wall
I am trying to make a wall that the player can NOT get past. The player moves with 32 pixels per button-press, as soon as there's collision it shouldn't be able to move any further.
Below is the current code, any ideas?
void OnTriggerEnter(Collider otherObject)
{
if (otherObject.tag == "walls-blocks")
{
Debug.Log("(PlayerHitWall) Player hit: " + otherObject.name);
if (w == true)
{
gameObject.transform.position = lastPosition;
}
}
}
By the way "w == true" refers to keyboard input booleans:
if (Input.GetKeyDown("w"))
{
w = true;
s = false;
a = false;
d = false;
}
and lastPosition refers to: lastPosition = gameObject.transform.position; (in the Update function)
Answer by Berenger · Feb 06, 2012 at 03:18 PM
What isn't working ?
Basically, in those case you want tto know the penetration when the player goes where he isn't supposed to, then reject him from that amount, with a reflection upon the normal of the wall, so it don't just freeze. If your translation amount is constant, you're probably doing the right thing.
Answer by sjefke31 · Feb 06, 2012 at 08:33 PM
Well, the problem is not really that the collision doesn't work. But the way it works in my method. The way I did it is as follows: 1) Press W 2) The player jumps up Vector3(0,0,0) to Vector3(0,0,0.64) 3) Collision detects there's another Collider 4) Due to collision it tells the player to move back: Vector3(0,0,-0.64)
This doesn't go smooth, you can see the player move to the collision position and jump back.