Collision does not work, player can walk through walls

So I have a character with a non-kinematic rigidbody that does not use gravity. The character also has a box collider around it that is not a trigger. My character is walking along a plane with a mesh collider, turning on gravity does not cause them to fall. I am trying to put box colliders along the sides of the plane to stop my player from walking off. I have tried both using root motion for movement of my player/character as well as using translate and rotations on the transform of my player. I most recently tried a character controller to see if it would make any difference but to no avail.

I have a debug print out for the collison. The player collides according to the debug log in my oncollisionenter but is able to walk right through the wall out into oblivion. How do I get collision to work? My player is able to walk through my invisible wall boundaries, trees, enemies, everything. I have also tried discrete, continuous, and continuous dynamic on the rigidbody. I just want my character to not be able to move through colliders and be forced to move around them or walk back from the world boundaries.

My current movement code for those asking:

[SerializeField]
public float speed=2f;
[SerializeField]
private float defaultSpeed = 2f;

[SerializeField]
private Transform playerGraphic;
  
private Animator controller;

private void Start{
   controller = this.gameObject.GetComponent<Animator>();
}

private void Update() {
    if (!controller.GetBool("dead"))
    {
             Movement();
             if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S))
            {
                controller.SetBool("walk", true);
            }
           
      }
}
private void Movement()
{

        //Player object movement
        float horMovement = Input.GetAxisRaw("Horizontal");
        float vertMovement = Input.GetAxisRaw("Vertical");


        if (horMovement != 0 && vertMovement != 0)
        {
            speed = defaultSpeed - .5f;
                       

        }
        else
        {
            speed = defaultSpeed;

        }
        transform.Translate(transform.right * horMovement * Time.deltaTime * speed);
        transform.Translate(transform.forward * vertMovement * Time.deltaTime * speed);
        
        //Player graphic rotation
        Vector3 moveDirection = new Vector3(horMovement, 0, vertMovement);
        if (moveDirection != Vector3.zero)
        {
            Quaternion newRotation = Quaternion.LookRotation(moveDirection);
            playerGraphic.transform.rotation = Quaternion.Slerp(playerGraphic.transform.rotation, newRotation, Time.deltaTime * 8);

        }
    }

What's the actual movement code you're using? Clearly if you're setting transform.position directly then you're not giving the physics engine chance to react to the collision. Have you tried setting the rigidbody velocity instead?

–

you can only use the layer mask system. maybe you could temporarily switch a collider to a special "ignore" layer when you want it to be ignored?

–

3 Answers

3

do the walls have colliders?

My walls definitely have colliders. I put debug log code in both the OnCollisionEnter and OnCollisionExit. Both of them get triggered as I hit and then walk through and out of my walls. Same goes for my enemies and the trees in my game.

–

I thought of it. But this would also get the shooter colliders to be ignored by other shooter's projectiles (while shooting). I will try using using particle triggers instead of collision. If it works, I will add another reply here. Thanks Richard.

–

Perhaps you’re moving too quickly for the fixed delta time. Try changing the time step by going into

Edit → Project Settings → Time

and changing the Fixed Timestep to maybe 0.01 (100 FPS). If that doesn’t work, something’s wrong with your colliders.

EDIT: Also make sure that Is Trigger is unchecked with colliders you want to stop your player.

@TheSOULDev I tried this and sadly it doesn't work either. As for the fps I'm getting, it is probably more around 25,30(really need a new graphics card and computer in general). I tried changing it 0.03 as well as 0.01, no difference. isTrigger is not checked on anything I am trying to get to stop my player.

–

No luck, particle triggers are useful for changing particles as they enter or exit colliders, but I would need to get the collider they enter to achieve what I need, and they are not provided.

–

I was running into a similar issue so what I did is five groups/layers 1. projectiles that can hit everything, except other projectiles 2. enemy projectiles that can hit only allies 3. allied projectiles that can hit only enemy's 4. allied unit layer 5. enemy unit layer and when the range/vicinity of the shooting unit is left by the projectile, I switch the projectile from B or C to A so that I enable team damage :)

–

I understand your player movement is through character controller. So now I want you to try adding mesh collider component to your walls. Make sure you check on “Convex” option in the mesh collider.
If your walls have a box collider, also check on “Is Trigger” option in it.
Try it and let me know if this worked.

bump, i would too like this.

–