- Home /
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 ins$$anonymous$$d?
Hi @tanoshimi ! I have posted my code above. I am using translate for movement and am setting the rotation directly. I have not tried setting the rigidbody velocity ins$$anonymous$$d. I can try that and see if it helps.
Welp @tanoshimi , I tried setting the velocity for movement ins$$anonymous$$d of using translate, still does not work.
Answer by Ratchet_Ritz · Jul 29, 2017 at 09:49 PM
do the walls have colliders?
$$anonymous$$y 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.
Answer by TheSOULDev · Jul 29, 2017 at 10:54 PM
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.
Answer by PersianKiller · Jul 30, 2017 at 03:38 AM
I think that's because of that your moving your character with transform.Translate when you use it your collider won't collision with others change it to GetComponent().velocity = new Vector2 (MoveSpeed*Input.GetAxisRaw("Horizontal"),GetComponent().velocity.y); it might help you.
@Persian$$anonymous$$iller This still doesn't work. I am able to have my player move this way, but it still walks right through colliders. I tried doing all of the movement in fixed update and I tried changing the speed as well, still does not work. I also tried changing the settings on the rigidbody for collision. I tried discrete, continuous, continuous dynamic, no difference. I honestly don't know what to do.
Hi!! Really sorry to resurrect a very old question, but did you manage to get this fixed? I'm in the exact same position with the same issue at the moment, and there's only so many times I can hear people say "have you added a collider to your walls?" before I lose my $$anonymous$$d completely :)
Check out physics collision matrix in Edit->ProjectSettings->Physics->Scroll down
. You have to have the crossing between layers of the player and the walls to be checked.
Answer by Abirami-Govindarajan · Jul 31, 2017 at 07:07 AM
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.