x


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)

more ▼

asked Feb 06 '12 at 02:27 PM

sjefke31 gravatar image

sjefke31
11 10 11 11

(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

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.

more ▼

answered Feb 06 '12 at 03:18 PM

Berenger gravatar image

Berenger
11k 12 19 53

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 06 '12 at 08:33 PM

sjefke31 gravatar image

sjefke31
11 10 11 11

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2497
x804
x100

asked: Feb 06 '12 at 02:27 PM

Seen: 1086 times

Last Updated: Feb 06 '12 at 08:33 PM