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);
}
}
}