- Home /
Suck gameobjects to a sphere
I have created a sphere and I want to give it its own gravity (I mean when GameObjects with rigged bodies come near it they get pulled towards the sphere) (some blackhole effect)
I do not know here I need to start looking for this.
I'm hoping someone can tell me where I need to look for this, or give a example c# script
Greetings Thyvo
Answer by Xeong-Hu · Mar 23, 2015 at 09:52 AM
Try this out man!
using UnityEngine;
using System.Collections;
public class Tester : MonoBehaviour {
public GameObject PullOBJ;
public float ForceSpeed;
public void OnTriggerStay (Collider coll) {
if (coll.gameObject.tag == ("Pullable")){
PullOBJ = coll.gameObject;
PullOBJ.transform.position = Vector3.MoveTowards
(PullOBJ.transform.position,
transform.position,
ForceSpeed * Time.deltaTime);
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Depending if you're using a Sphere or a Circle, add Sphere/Circle Collider to your Vortex GameObject, Then Duplicate that Sphere, add it as Child of your Vortex GameObject, and delete the Mesh renderer within it.
Then add the script I just posted into the Vortex's Child Object.
Next set that Child Object as a Trigger and name it as PullForce or Something.
Then, the rest is simple.
Duplicate as many PullForces Objects as you want and increase the Radius as you continue.
Now as the gameobject get's closer, Force's pull will become stronger. And as it falls away from it, vice versa.
Tested it, and it worked fine.
Hope that helps dude!
Answer by gameplay4all · Mar 23, 2015 at 10:12 AM
This is exactly what you need!
https://www.youtube.com/watch?v=gHeQ8Hr92P4
https://www.youtube.com/watch?v=TicipSVT-T8
Credits to Sebastian Lague, he makes awesome tutorials!
Answer by schaddem · Apr 03, 2012 at 11:24 PM
I'd try it like this: make a script you attach to every gameobject you want to check. i'll call your gravity object planet. 1. check the distance between rigid body and planet 2. If distance is smaller than the range prepare to affect it with a force 3. Calculate the vector between the 2 positions so we know where we're being drawn 4. Add the force based on distance
var distance = Vector3(transform.position, planet.transform.position);
if (distance<range)
{
var dif = planet.transform.position-transform.position;
//thats the vector3 that gives us the direction where we're being drawn to
var maxforce = 1000;
var forcestrength = distance/range * maxforce;
// (distance/range) will give a porcentage 0.0-1.0. we calculate how much porcent of the maximum force should be applied based on distance
rigidbody.AddForce(direction, ForceMode.Force);
}
note: this code is from the top of my head and more than likely to contain errors, but you get to see what relevant functions are called. You might need an other ForceMode (or AddConstantForce or something like that), but I think ForceMode.Force is just right for gravity. Also the forcestrength is interpolated in a linear fashion (x=y) and it should probably be Power of two (x=PowerOfTwo(y)). And maxforce=1000 might be way too high/low. Other thing to note is that this is physics code and should be contained in FixedUpdate().
Your answer
![](https://koobas.hobune.stream/wayback/20220613065839im_/https://answers.unity.com/themes/thub/images/avi.jpg)