- Home /
collisions not working
my scene contains 2 boxes and a floor.
my character can stand on the floor and boxes but if i run at the side of the boxes the character goes through.
im not sure what im doing wrong.
private void move2()
{
// Debug.Log("move2 called");
if (Input.GetKey(KeyCode.W))
rb.MovePosition(transform.position + transform.forward * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
rb.MovePosition(transform.position - transform.forward * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
rb.MovePosition(transform.position - transform.right * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
rb.MovePosition(transform.position + transform.right * speed * Time.deltaTime);
if (Input.GetKeyDown("space"))
jump();
},i don't understand why my player doesn't fall through the floor but can run through walls.
my scene contains a floor and 2 boxes, i can stand on the boxes and the floor but if i try walking into the boxes my character goes strait through.
i was originally using transform.translate but a quick google search made me try rb.moveposition instead it works the same except i still have the same problem. its obviously colliding because it wont fall through the floor but i have no idea why the walls dont stop it.
private void move2() {
// Debug.Log("move2 called");
if (Input.GetKey(KeyCode.W))
rb.MovePosition(transform.position + transform.forward * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
rb.MovePosition(transform.position - transform.forward * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
rb.MovePosition(transform.position - transform.right * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
rb.MovePosition(transform.position + transform.right * speed * Time.deltaTime);
if (Input.GetKeyDown("space"))
jump();
what am i doing wrong
The problem is probably how the colliders and the rigidbody is set up. The questions might be tedious and too basic, but there are many options, so please bear with me. The questions are:
- does the box have a collider? (if not, add one)
- is the trigger checkbox on the box's collider unchecked? (if no, uncheck it)
- does your character have a collider? (if not, add one, but not a $$anonymous$$eshCollider)
- What type? ($$anonymous$$eshColliders tend to detect collisions more poorly)
- is your character's rigidbody set to kinematic? (if yes, you may want to uncheck it, but it depends on what you want)
- does your character's rigidbody use gravity? (if not, then that's why you can "stand" on the box)
- what are your character's interpolate and collision detection settings?
- how much is the speed
value in the above code? If it is very high, you will need to set collision detection to continuous dynamic
Additionally, you might want to look into the CharacterController component: this handles character movement and collisions itself, and does not need (and should not have) a rigidbody.
Finally, don't set transform.position
if you have a rigidbody on the game object, because it causes performance issues with the physics engine. If you want to teleport your character set position with rigidbody.position
- this will ignore colliders when moving (unlike $$anonymous$$ovePosition()
, which checks for collisions, although sometimes poorly).
Answer by wsimm101 · Jul 30, 2018 at 07:02 AM
I'm not pro but apparently sometimes the update function goes quicker and it goes through the thing. Maybe make the collier bigger? or add a raycast? or try layer or tag?