简体   繁体   中英

How to prevent character from going through walls. Unity

I'm new to unity here and I have a capsule for a player and a moving wall. When ever the player touches the wall, it can just go through and walk out the other side, and I kind of don't want that. The screenshot below demonstrates what I mean.

Player in moving wall

1个

I tried to use rigid body and collisions to help give me a solution, but that didn't stop the problem. I'm now not really sure what the best thing is to do.

Rigidbody should be able to do the trick. You said that it did not work, so here are a few things you need to make sure you have done for the Rigidbody collisions to work properly:

  • Give the player a Rigidbody component
  • Give the player a collider (best use capsule collider)
  • Give the wall a collider (it looks like a box collider would work best)
  • When you move the player, move them with the function Rigidbody.MovePosition(newPos) or by setting the velocity with Rigidbody.velocity = new Vector3(newVelocity)
  • The "Is Kinematic" checkbox of the player's Rigidbody component is un-checked (set to false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM