Search Unity

Running on a ball like a circus bear?

Discussion in 'Scripting' started by Shobosa, Dec 5, 2019.

  1. Shobosa

    Shobosa

    Joined:
    Jul 8, 2015
    Posts:
    18


    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlanetScript : MonoBehaviour {
    5.     public float gravity = -12;
    6.     public void Attract(Transform playerTransform)
    7.     {
    8.         Vector3 gravityUp = (playerTransform.position - transform.position).normalized;
    9.         Vector3 localUp = playerTransform.up;
    10.         playerTransform.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);
    11.         Quaternion targetRotation = Quaternion.FromToRotation(localUp, gravityUp) * playerTransform.rotation;
    12.         playerTransform.rotation = Quaternion.Slerp(playerTransform.rotation, targetRotation, 50f * Time.deltaTime);
    13.     }
    14. }
    15.  
    player code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerGravityBody : MonoBehaviour {
    5.     public PlanetScript attractorPlanet;
    6.     private Transform playerTransform;
    7.     void Start()
    8.     {
    9.         GetComponent<Rigidbody>().useGravity = false;
    10.         GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
    11.         playerTransform = transform;
    12.     }
    13.     void FixedUpdate()
    14.     {
    15.         if (attractorPlanet)
    16.         {
    17.             attractorPlanet.Attract(playerTransform);
    18.         }
    19.     }
    20. }
    21.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    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.)
     
    Joe-Censored likes this.
  3. Shobosa

    Shobosa

    Joined:
    Jul 8, 2015
    Posts:
    18