1

I have a simple setup. A player with a camera, a plane that the player walks on, and two walls with box colliders.

THE PROBLEM When moving in one direction (X or Z-Axis), the player stops when touching the wall. But in the corner where the two walls intersect, when moving diagonally with W and D keys for example, the player moves through or slips through. How can I prevent this, as I have been struggling with it for some time.

Below is my Character Controller Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerController : MonoBehaviour
{
    public float p_moveSpeed = 10f;

    void Start()
    {

    }

    void Update()
    {
        float horizontalInput = Input.GetAxisRaw("Horizontal");
        float verticalInput = Input.GetAxisRaw("Vertical");

        Vector3 moveDirection = new Vector3(horizontalInput, 0f, verticalInput);
        transform.position += moveDirection * p_moveSpeed * Time.deltaTime;
        moveDirection.Normalize();
    }
    void LateUpdate()
    {
        Vector3 position = transform.position;
        transform.position = position;
    }

}

Below are the screenshots I took where I was against the wall, another screenshot where I got through the two walls intersecting and keep in mind both walls have a box collider on it.

Here I am pressed against the wall, walking straight into it...

Whilst moving diagonally I slipped through the walls where they are intersecting, I drew a blue circle around the area where my player is...

The INSPECTOR properties of the cube which acts as my wall...

The INSPECTOR Properties of my capsule player...

It is as if my player walks through the intersection, how I am able to rectify it, I do not know. For those willing to assist, thank you in advance. I have watched numerous videos and tutorials about collision but not once have I seen something like this where the player is able to stop when colliding into the wall but goes through at the intersection walls (with colliders attached)...

  • I have tried putting spheres in the corner with a collider.
  • I have tried making both my walls STATIC in the HIERARCHY.
  • I have tried setting both walls as KINEMATIC
  • I have tried using every collider possible.
  • I have tried adding more Rigidbodies.
  • With the Rigidbodies, I changed the COLLISION DETECTION various times but was unsuccessful.
  • I have tried resizing my player and my walls but nothing happened, still the same problem.
6
  • 3
    Using Physics to move would most likely resolve this issue. Update can run multiple times between physics frames. All physics takes place during the FixedUpdate frame. Commented Sep 16, 2024 at 19:00
  • The answer is because you dont use physics to move, as @hijinxbassist says, if you move with physics collisions are taken into account but you are in effect teleporting, and it assumes you know best. Commented Sep 16, 2024 at 19:04
  • If you want other objects to affect Player, then Player needs a rigidbody and non-kinematic. What the others are saying is correct. One addition, is that you need to move the player through rigidbody.velocity which will take into account the fixed time delta, the direction set by your input and any other speed factor you want. Or if you just want sth nice and simple without too much fuss, just use a CharacterController component. iirc, it creates a capsule collider on its own for humanoids and adds a rigidbody as well. Commented Sep 16, 2024 at 19:10
  • oh and this manual page is your friend. this is the place to learn which interactions between two entities would produce a collision. Commented Sep 16, 2024 at 19:17
  • What exactly is your LateUpdate supposed to do...? In general if you want to use physics then you shouldn't move your object via transform but rather Rigidbody ... Commented Sep 16, 2024 at 19:25

1 Answer 1

1

Collisions do not always work if you move an object by repeatedly setting its position. You should set the object's rigid body's velocity component instead if you want the built in collision resolution system to work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.