ssatguru / BabylonJS-CharacterController

A CharacterController for BabylonJS
Apache License 2.0
217 stars 46 forks source link

How cam I programmatically force an avatar to fallFall? #74

Closed warrenchopin closed 2 months ago

warrenchopin commented 2 months ago

Hi again,

I am implementing collisions of multiple avatars individually controlled by CharacterController and a collided avatar bounces into the mid air above the ground sometimes. The avatar in the mid air stops in the mid air until new user input . I want to make the avatar freely fall to the ground right after collision in this case.

I tried to set CharacterController._inFreeFall= true right after collision, but am error was raised as following:

CharacterController.ts:952 Uncaught TypeError: Cannot set properties of undefined (setting 'y')
    at CharacterController._doMove (CharacterController.ts:952:32)
    at CharacterController._moveAVandCamera (CharacterController.ts:779:22)
    at CharacterController._renderer [as callback] (CharacterController.ts:1744:39)
    at e.notifyObservers (observable.ts:393:49)
    at t.render (scene.ts:4601:39)
    at Function.<anonymous> (

How cam I programmatically force an avatar to fallFall?

I really appreciate your help.

ssatguru commented 2 months ago

@warrenchopin try setting _freeFall to true instead of false. Let me know if that works

warrenchopin commented 2 months ago

The above error was raised when _inFreeFall= true. I edited the posting correctly.

I tested both _inFreeFall= true and false cases again for sure. The error was raised only when _inFreeFall= true

I thought about three scenarioes to solve this problem.

  1. a new command freeFall()
  2. a new command moveToPosition(pos, freeFall)
  3. a new command moveDiff(vDiff)

The best solution for me is 3. moveDiff(). It is a general function that can be used in many different situations. The problem happened after I overwrote avatar.mesh.position at collision. Overwriting position is pretty aggressive way possibly disrupting CharacterController. moveDiff() will give a way to avoid overwriting mesh.position. Is there a similar function available now?

Thanks again.

ssatguru commented 2 months ago

You are right in that we do need a function for fall. Just like we can do jump programmatically , we should be able to do fall too. This will be usefull for use cases like yours, moving platforms etc. Let me work on this.

In the meantime try setting CharacterController._grounded to false after collision and let me know if that works.

warrenchopin commented 2 months ago

It worked when I set the two variables together.

cc._inFreeFall= true 
cc._grounded= false

The collided avatar free falls naturally now: The problem solved. Thanks you so much for your help.

ssatguru commented 2 months ago

Nice. I just published a new version (0.4.4) with a cc.fall() function to force a fall. This should also play a fall animation while falling.

warrenchopin commented 2 months ago

@ssatguru: The error was raised later again. I set only cc._grounded=false at this time, and the error disappeared again:

cc._grounded= false

I am not sure whether CharacterController is unstable, or my program is unstable in this case. I will report here if something is wrong again.

Thanks again.