- Home /
3D Gravity towards one object
I am already aware of unity's directional gravity from the world properties, and have been experimenting quite a bit with it recently... However, I wanted an object to attract all objects in a specific radius (trigger?) around it. Basically, it is a gun that fires a small GameObject that will pull everything in area towards it... Is it possible?
Thanks in advance!
Answer by Tomer-Barkan · Dec 19, 2013 at 11:54 AM
Assuming all the objects that need to be pulled have rigidbodies (and colliders), you can use Physics.OverlapSphere to detect all the objects within a radius, and then manually apply the force using Rigidbody.AddForce.
public class ObjectPuller {
public float pullRadius = 2;
public float pullForce = 1;
public void FixedUpdate() {
foreach (Collider collider in Physics.OverlapSphere(transform.position, pullRadius) {
// calculate direction from target to me
Vector3 forceDirection = transform.position - collider.transform.position;
// apply force on target towards me
collider.rigidbody.AddForce(forceDirection.normalized * pullForce * Time.fixedDeltaTime);
}
}
}
Interesting to know
does Physics.OverlapSphere create doubles? like collider triggers?
Physics.OverlapSphere simply returns an array of all the colliders within range. If an object has two colliders it might return them both, but I don't think that is possible.
I mean if it's creating doubles like colliders
as if you have too many colliders together you get error doubles.
and it says new doubles are ignored.
I never heard of any such problem. Each collider will be returned once regardless of its distance from other colliders.
Answer by thk123 · Dec 19, 2013 at 11:49 AM
Perfectly possible, but not using unity's simple gravity property, which is designed explicitly for the case of pulling stuff in one, fixed, direction.
Instead, you need to apply a force to all objects you want to attract. You can do this in a number of ways, but one way is to attach a attracted
behaviour to each object you want to pull towards. It could look something like this:
class Attracted : MonoBehaviour
{
public GameObject attractedTo;
public float strengthOfAttraction = 5.0f;
void Start {}
void Update
{
Vector3 direction = attractedTo.transform.position - transform.position;
rigidBody.AddForce(strengthOfAttraction * direction);
}
}
Making sure, when you attach it to an object, you set the attractedTo
to be the object that you want to attract them to.
The above script by @thk123 worked great. Thank you very much! Now here's a slightly updated version for people having trouble with it, adding gameObject to rigidbody and a FixedUpdate ins$$anonymous$$d of an Update. This is the version working best for me:
c# script below named "AttractedToObject.cs"
using UnityEngine;
using System.Collections;
public class AttractedToObject : $$anonymous$$onoBehaviour
{
public GameObject attractedTo;
public float strengthOfAttraction = 5.0f;
void Start ()
{
}
void FixedUpdate ()
{
Vector3 direction = attractedTo.transform.position - transform.position;
gameObject.rigidbody.AddForce (strengthOfAttraction * direction);
}
}
gameobject.rigibody is no longer used so you need to use gameObject.GetComponent().AddForce (strengthOfAttraction * direction);
Besides that it is perfect!
Hi, your script is running great. But i want to move around this "attraction object" (my player). $$anonymous$$y Player is a prefab and i set other cubes to be attractedTo
Player prefab. the problem is that cubes are always attracted to starting position of the players prefab(which is then change later in game by WASD). do anyone know how to fix that. Thanks.
Answer by tiagorpg · Jul 15, 2018 at 12:35 AM
i did this, but to a different need, i created a planet, but now i need to make the ship remain upwards related to the center of the planet, how to do this withouth change the y orientation of the ship ?