Search code examples
unity-game-enginerigid-bodiesanimator

Unity Camera Bounces with Sphere Animation when trying to apply gravity with Rigidbody


I have an empty Object that has, among other things, a third and first person camera, and a sphere with a bouncing animation attached to it. I'm try to get it so that the sphere experiences gravity, but I'm having a lot of problems.

What I want is gravity on my sphere and the camera to not bounce with the ball.

Here a couple scenarios I've tried, and their outcomes:

1) When I attach a rigid body to the the Sphere (child of the empty Object) with apply root motion true or false, my character experiences no gravity. The ball bounces, but the camera follows the ball without bouncing with the ball. It also follows if i press the space button (to jump) The whole problem is i need gravity to fall back down when i jump. Right now i just go up.

2) When I attach a rigid body to the empty object (the parent), I gain gravity, but now my camera bounces with the ball, and if the ball tumbles and rotates, the camera rotates with it.

Is there a way to get the best of both worlds?

i.e: Ball experiences gravity, but the camera does not. The camera simply follows around the ball from first or third person view without bouncing.

Thanks so much


Solution

  • For me, I would like to create a Vector3D variable to record the offset between the ball and the camera. And you simply add the offset to your camera each time so your camera can chase the ball without bouncing with it. For example:

    Vector3D offset  = cameraPos - ballPos;
    //...
    CameraPos = ballPos + offset;
    

    If you don't want the camera moving with ball's gravity, you can block the change of Y axis.