Running on a ball like a circus bear?

I am trying to make an animated model run on top of a beach ball. I have tried multiple code and I can’t get the animated character to stay on top of the ball. He either runs around the ball or stays on the same spot on the ball while it rotates. and the ball ends up running him over. Please help is you can, been stuck forever.

Ball Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlanetScript : MonoBehaviour {
    public float gravity = -12;
    public void Attract(Transform playerTransform)
    {
        Vector3 gravityUp = (playerTransform.position - transform.position).normalized;
        Vector3 localUp = playerTransform.up;
        playerTransform.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);
        Quaternion targetRotation = Quaternion.FromToRotation(localUp, gravityUp) * playerTransform.rotation;
        playerTransform.rotation = Quaternion.Slerp(playerTransform.rotation, targetRotation, 50f * Time.deltaTime);
    }
}

player code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerGravityBody : MonoBehaviour {
    public PlanetScript attractorPlanet;
    private Transform playerTransform;
    void Start()
    {
        GetComponent<Rigidbody>().useGravity = false;
        GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
        playerTransform = transform;
    }
    void FixedUpdate()
    {
        if (attractorPlanet)
        {
            attractorPlanet.Attract(playerTransform);
        }
    }
}

I am going to go out on a limb here and say you won’t be able to get this truly controllable with just physics. You’re going to have to either fake the physics and animation yourself, or else help the physics a lot by guarding you back on with automatic corrective forces using some filter table or heuristic to keep the player on the ball.

The reason I say this is that I wrote a fully-3D lunar lander style flight control system in Jetpack Kurt Space Flight, and playing it is HARD. That only involves keeping your spaceship balanced on top of the single main retro rocket engine, and there are no rolling moments, and even that is really hard to play. I ended up writing several little filter table helpers and correction forces to help newer players keep upright. Once you turn off those helpers, the game is fully playable, just using pure unadulterated physics, but oh man that is tough! (But it’s my favorite way to play now.)

1 Like

Thanks for the input. I was looking online and came across this tutorial.

I had to you google translate to read it. It looked like he got the function and made it work. When I followed the driections I didn’t get the same results or a functioning guy on ball. Maybe I did it wrong.