0
\$\begingroup\$

I'm trying to create a simple platforming game. I have an animated platform moving in 2 opposite directions. I use this simple code to make the character move along with the platform while he is standing on it:

public class PlatformMoving : MonoBehaviour
{
    private void OnTriggerEnter(Collider other) => other.transform.parent = transform;

    private void OnTriggerExit(Collider other) => other.transform.parent = null;
}

The character moves with CharacterController.Move(). When he stands still on the platform and movement vector equals to Vector3.zero, he moves in the direction opposite to the platform movement direction and can even fall down from the platform. But if I comment charController.Move(movement) line in a character motion script, everything works properly and the character does not move when he shouldn't do that. How to solve it?

\$\endgroup\$
7
  • \$\begingroup\$ Whenever the character lands on the platform, you want it to be a child of the platform? \$\endgroup\$
    – Sean Carey
    Commented Jan 22, 2019 at 21:29
  • \$\begingroup\$ @Sean Carey Actually, I didn't devise this method myself. I just saw it in YouTube video, and it worked properly. \$\endgroup\$
    – zergon321
    Commented Jan 22, 2019 at 23:46
  • \$\begingroup\$ Ok, but you have to understand the code. Can you post the YouTube video your following? \$\endgroup\$
    – Sean Carey
    Commented Jan 23, 2019 at 0:00
  • \$\begingroup\$ When doing movement, try moving character only if its movement vector != Vector3.zero. Like, try adding check if (movement != Vector3.zero) { charController.Move(movement); } \$\endgroup\$
    – eLTomis
    Commented Jan 23, 2019 at 14:01
  • 1
    \$\begingroup\$ @zergon321 did that answered your question? If yes, I will post it as answer. \$\endgroup\$
    – eLTomis
    Commented Jan 23, 2019 at 14:50

1 Answer 1

3
\$\begingroup\$

Welcome to the game developers hub, @zergon321!

Eventough, I've answered your question in comments, other people might be interested as well, so I'm putting the answer over here.

The answer

Try checking if there's any movement input before applying it with CharacterController. So for example:

if(movement != Vector3.zero)
    charController.Move(movement);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .