TheAdmiringDeveloper
TheAdmiringDeveloper

Reputation: 21

I can't detect collision infront of the player

Basically if there's collision infront of the player it shouldn't go forward anymore, but the player goes forward no matter what.

if (Input.GetAxis("Vertical") > 0 &&
        !(Physics.Raycast(PlayerObject.transform.position,
                          Vector3.forward,
                          PlayerObject.collider.collider.bounds.size.z * 1.05f)))
    {
        PlayerObject.transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
    }

Upvotes: 0

Views: 42

Answers (1)

David
David

Reputation: 10708

The problem here is that you're performing a Raycast using physics, so anything not enabled for physics won't show up. You're also using Vector3.Forward instead of PlayerObject.transform.forward, so you're getting the global forward (0,0,1) instead of the player's actual vector. Furthermore, but using the player's position, it won't notice, say, a low or high-hanging wall.

Note there is also the built-in CharacterController component to handle this sort of behaviour, as well as intelligently handling Ramps and Steps.

Upvotes: 1

Related Questions