Home > database >  Unity Player passsing through objects
Unity Player passsing through objects

Time:10-23

player and object both have colliders and rigidbodies, object has position and rotation locked, player has only rotation locked. When the player goes to the blocks, the player goes through the blocks, although they do give a bit of resistance. To move the player im setting the rigidbody's velocity, and doing that in FixedUpdate. i have no idea why this is happening, any ideas?

main part of the code is: rigidBody.velocity = new Vector3(direction.x, rigidBody.velocity.y (-Gravity * Time.deltaTime), direction.z); (direction is determined by the WASD keys, and i'm using my own gravity)

CodePudding user response:

I guess it has something to do with what the documentation says "In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour".

Try to use AddForce() or similar functions to alter the properties of the rigid body. Colliders etc will then work as expected.

CodePudding user response:

First of all, you do not need to multiply the velocity by time.DeltaTime, because you are moving your object in the FixedUpdate() method; Which uses fixed time intervals since the physics engine does not run in sync with the regular game engine. Also, both objects do not need rigidbodies in order to collide with one another. I suggest looking at your collision matrix in settings and verifying that everything you need collision for is checked correctly. As others have said as well, check your kinematics on the rigidbody.

A last suggestion for working with your own gravity. Do not change the actual gravity value of the game engine. It is typically recommended that you use a multiplier variable and apply it to the constant gravity already set by the physics engine. If you are completely editing the gravity, than maybe consider using a character controller instead.

  • Related