- Home /
How do I stick an object to another without stopping it's movement (not like glue)
Hi, I'm trying to make an AR cube game where the cube is kind of like the planet but whenever I rotate the cube, a sphere on top will fall towards the gravity below but it still has to stick on the cube world.
Any idea on how to make it without sticking like glue?
What exactly do you mean by stick on the cube world? Should it behave like a real planet where the gravity is always pulling towards the cube center, or some different behavior?
Answer by Deutschland1000 · Aug 06, 2020 at 10:19 AM
One attempt would be to add a force always to the center of the cube.
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlanetGravity : MonoBehaviour {
public Transform gravityCenter;
public float gravityMultiplier;
private Rigidbody rigidBody;
void Awake()
{
rigidBody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rigidBody.AddForce((gravityCenter.position - transform.position).normalized * gravityMultiplier);
}
}
Just add this script to your sphere and drop the cube to the gravityCenter. You can also add a physics material to make it bounce a bit and play around with the gravityMultiplier value.
Your answer
