Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Distance between two objects while attracting each other

Discussion in 'Physics' started by Artist_Dragon, Dec 12, 2018.

  1. Artist_Dragon

    Artist_Dragon

    Joined:
    Dec 7, 2018
    Posts:
    9
    Good morning!! (Sorry for my grammar mistakes, English is not my native language)

    I'm working on an app that shows a great amount of nodes holding data. These nodes build a relationship if they have common information. In order to show that visually, I want to attract these nodes to each other. But I dont want them to collide entirely.

    I have done some research and I found another user with a similar problem. I tried what he was scripting and it does maintain a distance between these objects + with a smooth animation, but I dont know how to attract them in the first place in a determined distance.

    This is the code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Follow : MonoBehaviour {
    6.  
    7.     public Transform leader;
    8.     public float followSharpness = 0.1f;
    9.  
    10.     Vector3 _followOffset;
    11.    
    12.  
    13.     void Start()
    14.     {
    15.         // Cache the initial offset at time of load/spawn:
    16.         _followOffset = transform.position - leader.position;
    17.     }
    18.  
    19.     void LateUpdate()
    20.     {
    21.         // Apply that offset to get a target position.
    22.         Vector3 targetPosition = leader.position + _followOffset;
    23.  
    24.         // Keep our y position unchanged.
    25.         //targetPosition.y = transform.position.y;
    26.  
    27.         const float REFERENCE_FRAMERATE = 30f;
    28.  
    29.         // Then, in LateUpdate()...
    30.         float timeRatio = Time.deltaTime * REFERENCE_FRAMERATE;
    31.         float adjustedSharpness = 1f - Mathf.Pow(1f - followSharpness, timeRatio);
    32.  
    33.         transform.position += (targetPosition - transform.position) * adjustedSharpness;
    34.     }
    35. }
    Thanks a lot!!
     
  2. JuozasK

    JuozasK

    Unity Technologies

    Joined:
    Mar 23, 2017
    Posts:
    84
    Howdy!

    Not sure if this is what you are talking about, but if you want two objects to have a certain distance between them you can instead of target position have a float TargetDistance which would be a float of whatever distance you want them to be apart with.
    Then the current Distance would be "Vector3.Distance(Node1.position, Node2.position)" or something like that.
    If the CurrentDistance is larger than TargetDistance, you move your nodes closer together
    If the CurrentDistance is smaller than TargetDistance, you move them apart.

    You can look lnto Mathf.lerp, to linearly interpolate the position change to make it smoother.

    Looking at your code though, it seems that the distance the second object is going to follow the first object is determined by their starting positions when the script is booted up (in the Start() function)

    If you want to use the same script, I believe you should be able to multiply the _followOffset by a float to increase or decrease the distance the second object will be following the first one ([0..1] for reducing the distance and [1..infinity] to increase it)
    But beware, as this sort of solution will always be relative to the starting position of the first and second objects.
     
    Artist_Dragon likes this.
  3. Artist_Dragon

    Artist_Dragon

    Joined:
    Dec 7, 2018
    Posts:
    9
    Understood. Your suggestion sounds very close for what I'm looking for. I'll try it and reply here later with the results. Thanks a lot!!
     
  4. Artist_Dragon

    Artist_Dragon

    Joined:
    Dec 7, 2018
    Posts:
    9

    Ok, I tested it. About the distance, I'm aware. The nodes would appear after a "big bang" of data. So the starting distance wouldn't be that large. The second suggestion works very well. I've multiplied by 0.1F and the distance decreased. At first it wasn't close enough, but increasing the size of the nodes solves this problem. So, for now, it works for what I want

    Thanks again for your help!!