Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question What's the best way to keep objects a certain distance from each other?

Discussion in 'Scripting' started by xXGriMe18, Sep 18, 2023.

  1. xXGriMe18

    xXGriMe18

    Joined:
    May 28, 2021
    Posts:
    6
    I have a Sphere in my scene that represents a planet. I also have an asteroid that I want to move around the planet, however I dont want to use rotation to make it move. Rather when a bullet from the ship hits the asteroid I want it to be pushed around the planet, but always being at a certain distance from the planet itself.

    Currently this is what it looks like.

    https://imgur.com/a/DjvhnIz

    As you can see, when the asteroid is hit it floats off into space. What I'm trying to achieve is for the rock to always stay a certain distance from the planet, so when its hit, it will circle the planet instead of floating off.

    I have tried using a artificial gravity, but what ends up happening is the rock gets pulled into the grid around the planet and because the collider is not round on the asteroid it kind of just flops around when its hit. That's not really the effect I'm going for though. I'm looking for something that looks zero gravity, like shown in the gif above.

    Now I know I can get the distance between the asteroid and the planet by doing this.

    Code (CSharp):
    1. distance = (planet.transform.position - rock_1.transform.position).magnitude;
    And then I can use a if statement to check that the distance always stays the same, however how would I translate the distance its at to the asteroids vector3 position?

    Maybe I'm going at this all wrong, could someone help me out here?
     
    Last edited: Sep 19, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This is an exceptionally interesting problem.

    From your description it sounds like you want all the action to take place in a precisely thin shell around the planet, but to "make sense" in terms of velocities and bouncing and whatnot. Is that correct??

    Have you tried making each object be:

    - a Rigidbody at (0,0,0) (center of planet)
    --> zero drag
    --> zero angular drag
    --> locked in position x,y,z
    --> ignore gravity
    --> free in all rotational axes

    - a sphere and collider child GameObject out at the given range

    And see what that does? I think the Rigidbody center of mass will be correctly recomputed to the distant sphere... then you could try other convex colliders and see how they play when you add thrust or "pew-pew" to them and they whack into each other.
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    This assumes the planet is at position zero:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StayInOrbit : MonoBehaviour
    6. {
    7.     Rigidbody rb;
    8.  
    9.     float orbitDistance;
    10.     float orbitStrength=50f; // how forceful we should be in returning to the correct distance
    11.  
    12.     void Start()
    13.     {
    14.         orbitDistance=transform.position.magnitude; // assume our starting position is the correct distance
    15.         rb=GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         Vector3 orbitPosition=transform.position.normalized*orbitDistance; // the position we should be at
    21.         Vector3 fallVelocity=transform.position.normalized*Vector3.Dot(rb.velocity.normalized,transform.position.normalized); // our velocity towards or away from the planet
    22.         rb.AddForce((orbitPosition-(transform.position+fallVelocity))*orbitStrength); // push towards the correct distance
    23.     }
    24. }
    25.  
     
  4. xXGriMe18

    xXGriMe18

    Joined:
    May 28, 2021
    Posts:
    6
    This is perfect, thank you. Just out of curiosity have you worked on something like this yourself? I never would have figured out the math for this.
     
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Just keep going and I promise it will get easier. There was a time when I also would never had figured out the math for this.. :)