Hey guys, messing around in unity, making a little orbit game, you fire a projectile and it’ll orbit the ‘earth’ using AddForce (I know there’s other, better ways to do this, but I’m trying to figure it out this way, simply because I like to try and do as much as I can from my head, without following guides and such, it’s a little personal challenge, of sorts. I have 2 planets, and when I get closer to one, that will become the main target you will orbit around, works well, only thing is, I’d like to try to somehow have it so, the closer you get to the object, the more force is pushing you towards that object.
Here’s my script that works.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Attractor : MonoBehaviour
{
public GameObject earth;
public GameObject moon;
float G = 0.6f;
public Rigidbody2D rb;
void FixedUpdate()
{
rb = GetComponent<Rigidbody2D>();
Vector3 directionToEarth = rb.transform.position - earth.transform.position; //works
Vector3 directionToMoon = rb.transform.position - moon.transform.position;
float distanceToEarth = directionToEarth.magnitude; //works
float distanceToMoon = directionToMoon.magnitude;
if(distanceToEarth <= distanceToMoon)
{
rb.AddForce(earth.transform.position - transform.position); //need to figure out how to make the force more, when closer, smoothly.
} else if (distanceToMoon <= distanceToEarth)
{
rb.AddForce(moon.transform.position - transform.position);
}
}
}
What I want to do I think might be quite simple, I’m just not sure how to go about it, the closer you get to whatever you’re orbiting, the more force is added, so you can ‘slighshot’ so to speak, around objects better.
You are already using the distance in your code. Inversing that based on a maxDistance and using it as a factor should do the trick.
Somewhat like this:
float maxDistance = 10f;
factor = 1f + (Mathf.Clamp(10f - distanceToEarth, 0f, 10f) / maxDistance); //will give you a factor between 1 and 2
rb.AddForce((earth.transfrorm.position - transform.position) * factor);
In case of multiple planets attracting a things just add the forces together. If everything is correct it should just work. An apple falling to earth doesn’t magically know that earth is closer than moon. This also helps with smoothness. Any kind of ifs in motion equations are potential source of discontinuity and sudden jerks.
Just a little note, using “rb.transform.position” isn’t where the Rigidbody2D is. Using “.transform” on any component just gets the Transform on the same GameObject. The Transform on a GameObject that has a Rigidbody2D isn’t necessarily the same if you’re using interpolation/extrapolation. This is why you should use Rigidbody2D.position which is the real position of the physics body.
Would you be able to explain a little what this is doing exactly?, I’ve looked at it and fiddled with it for a while, I can’t seem to fully understand what is happening in that bit of code, I’d like to try to understand it better!
changing the numbers and messing around seems to not really have much, if any impact (Granted, I’m probably doing it wrong due to not understanding what it actually happening here)
It would be really appreciated!
I have changed and commented the code. Hope it helps.
float maxDistance = 10f; //This is the distance to the Object, at which an effect will start to take place.
//If you want gravity to increase at a Distance of 100f, insert 100f.
float multiplyer = 5f; //Just a variable to easily adjust the effect
factor = 1f + (Mathf.Clamp(maxDistance - distanceToEarth, 0f, maxDistance) / maxDistance);
//This is where the magic happens: maxDistance - distanceToEarth will increase the closer you get.
//It is clamped to stay positive and smaller then maxDistance.
//By dividing it with maxDistance it becomes a number between 0 and 1.
//This is added to one so that our factor will always increase (> 1f) the force
factor *= multiplyer; //Use the multiplyer to your interest
rb.AddForce((earth.transfrorm.position - transform.position) * factor);//Apply factorized force