3
\$\begingroup\$

I've got a player with a rigidbody/capsule collier (gravity enabled) and a plane with a box collider. When the player is on the plane, everything works as expected, the player can walk across the plane with no issue. However, when the player walks off the plane, I want the player to fall. However, what happens instead is that the capsule collider falls but the player just keeps walking in thin air. Why is this happening?

Problem: enter image description here

Colliders setup:

enter image description here

Code:

void Update () {
    i = 0.1f;
    transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + i);
}

Sorry if I'm doing something stupid, I'm new to game development/Unity, I usually program other things, not 3D games. Just expiermenting a bit to learn.

\$\endgroup\$
5
\$\begingroup\$

In your code you are moving the transform forward and keeping the y value the same at each call to Update(). By doing this your transform cannot drop because the y value is being reset at each call to Update(). A better way to move your character forward would be like this:

transform.position += transform.forward * Time.deltaTime * movementSpeed;
\$\endgroup\$
  • \$\begingroup\$ Out of curiosity, would applying a force to my rigidbody be an appropriate movement system? \$\endgroup\$ – Ashwin Gupta Oct 31 '16 at 5:25
  • \$\begingroup\$ @AshwinGupta It is better to move it as I have shown above unless it is for short spurts of movement. Continuous movement is best to have complete control over. If you apply force to a rigidbody for movement then you are basically doing one thing and waiting for the physics engine to handle the rest of the movement. It is really up to you and how you are wanting the behavior to be though. \$\endgroup\$ – saboehnke Oct 31 '16 at 13:48
  • \$\begingroup\$ ok thanks. That makes sense, I implemented your answer and it works great. \$\endgroup\$ – Ashwin Gupta Oct 31 '16 at 15:33
  • \$\begingroup\$ @AshwinGupta Good to hear! \$\endgroup\$ – saboehnke Oct 31 '16 at 19:05
4
\$\begingroup\$

Welcome to game development world. First of all I suggest to you not to move the player changing the transform values by code. Because, as you can see that can produce weird behaviours. So you have to use the Translate function. I will give you a really good resource, where you can read how to program a really cool control system and learn a bit of the "modus operandi" in Unity.

http://www.habrador.com/tutorials/programming-patterns/1-command-pattern/

Cheers!

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer, +1 , giving accept to saboehnke though since he was first. I hope you don't mind. \$\endgroup\$ – Ashwin Gupta Oct 30 '16 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.