Ball Rotate Around Cube

Good Afternoon,

I´m being fighting this days with a “simple” I think action.
I have a cube and a sphere in my scene, and I want that the ball moves around the cube, so after it´s on the edge, it will continue moving to the next side of the cube.

Do you have any tips of how can I do that?

Thanks you so much.

Take a look at this first

A little bit of modifying and your script is ready to go

using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public Transform target;     //Assign Cube in the Inspector
    public float speed = 20.0f; 
    void Update()
    {
        transform.RotateAround(target.position, Vector3.up, speed * Time.deltaTime);
    }
}

or if you want it to “stick” around the cube Add this script to the Cube :

using UnityEngine;

public class Gravity : MonoBehaviour {

    public float gravityRadius = 2.0f;
    public float gravityForce = 100.0f;

    public void FixedUpdate()
    {
        foreach (Collider collider in Physics.OverlapSphere(transform.position, gravityRadius)) {
            Vector3 direction = transform.position - collider.transform.position;
            collider.GetComponent<Rigidbody>().AddForce(direction.normalized * gravityForce * Time.fixedDeltaTime);
        }
    }
}

Note: in this case both object must have a Rigidbody. Cube rigidbody is IsKinematic = true and useGravity = false. Spehere useGravity = false… At least for the testing :slight_smile: