JLPM22 / MotionMatching

Motion Matching implementation for Unity
https://jlpm22.github.io/motionmatching-docs
MIT License
386 stars 40 forks source link

How to implement collision for obstacles? and what about vertical movement for slopes? #7

Open strangeways-dev opened 7 months ago

strangeways-dev commented 7 months ago

Thank you for the awesome repo.

How can we implement collision for obstacles? and what about vertical movements like ramps?

JLPM22 commented 7 months ago

Hi!

For the collision with obstacles, it is necessary to modify the SpringCharacterController included for the third-person view character. When updating the trajectories in the SpringCharacterController, this script should handle obstacle avoidance and not create trajectory points inside objects with colliders. For example, this could be implemented with raycasting. I will add an example for this use case in the near future.

For the vertical movements, I will investigate the best way to implement it soon (#5).

I will include all examples as soon as possible!

Thank you!

strangeways-dev commented 7 months ago

Thank you, I look forward to your implementation.

I will have a play about with it tomorrow and see what I can come up with in the meantime. If I come up with anything I will share it here for others to use until your implementation is available which I can only assume will be much better than mine.

Thanks again!

JLPM22 commented 7 months ago

Hi!

I've had some time to implement a simple example of how to add verticality + collisions. Please take a look at issue #5 for verticality. Regarding collisions, the same example, JLSceneColliders.unity, showcases how to implement it. As a summary, I created a new CollisionsSpringCharacterController.cs based on SpringCharacterController.cs that uses raycasts to detect whether there is an obstacle between the new position and the previous ones. Predicted and current positions are adjusted never to cross obstacles.

strangeways-dev commented 7 months ago

This is awesome, I can see how he snaps to the floor when he walks off the edge of the ramp though.

Making the Joe object have a Capsule Collider and a Rigidbody and making the SpringCharacterController use the Rigidbody instead of just moving the transform of Joe would be a good start in fixing this but I can see a few issues already that need to be resolved to make this possible. I would make the SpringCharacterController and MotionMatching objects both children of Joe and instead of using their transforms for the target positions I would create new Vector3 variables to use instead because the way it's coded if you make them children of Joe then when Joe moves they move and he would never reach his target.

Also the feet are away from the floor on slopes but I already have a solution for this. I've been working on a character system and I use the Unity Animation Rigging package to use IK on the feet to make them stick to the floor but that's half the problem because the collider also needs to be dynamic changing height and scale to allow one foot to be raised etc., which I calculate from the height distance between the two desired feet positions which are determined by Raycasting down from above where the feet should be before IK is applied. I have already coded this in another project I just need to integrate it in to this.

I will be able to do this, only the first problem will be tricky for me because I haven't fully wrapped my head around how your system works yet but I'm getting close to understanding.

I will make a Pull Request when I've finished for you to take a look at.

JLPM22 commented 6 months ago

Apologies for the delayed response. This is excellent feedback! You're right about the collider and rigidbody changes. I'm interested in learning more about your solution, and I'd be happy to review your pull request when it's ready. Let me know if I can clarify any parts of the system.