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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Distance between two changing floats?

Discussion in 'Scripting' started by TimBorquez, Oct 5, 2015.

  1. TimBorquez

    TimBorquez

    Joined:
    Mar 18, 2013
    Posts:
    81
    ok I'm too dumb at math and this is drivin' me crazy. I have found myself wanting the distance between two float numbers (eg positionX to targetX). I tried mathf.absolute and subtracting the two things. But I keep having trouble when the position and target change to and from negative. I'm looking for the right formula to find the distance between two floats regardless of negatives. I know I'm missing something simple . . .
     
  2. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    Mathf.Abs(targetX - positionX) will be the absolute difference, or distance, between two numbers - regardless whether either number is positive or negative. What are you using the result for? It's likely your trouble is due to your usage...
     
    art092, ErFer7 and RodrigoSeVeN like this.
  3. TimBorquez

    TimBorquez

    Joined:
    Mar 18, 2013
    Posts:
    81
    Well I have a Move() function where I pass in horizontal and vertical amounts from different input methods. And I'm TRYING to make a MoveTo(targetX, targetY) coroutine, where It moves on it's own, one axis at a time, to the target position. So it loops and sends the input to the move function while the distance is too large . .

    Code (CSharp):
    1. while(Mathf.Abs(targetPosX - transform.position.x) > .05f)
    2.             {
    3.                 Move (dirX, 0f);
    4.                 yield return null;
    5.             }
    But now that you confirmed it should be right, I'm thinking it's breaking because I'm actually overshooting the target and it's never stopping . . . I really should have done this better :O
    You were right, my implementation is wrong, I need to look into clamping my position somewhere or knowing if it overshoots a little bit, thanks
     
    Last edited: Oct 5, 2015
  4. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    Haha, mebbe - depending on the implementation of your Move function, and whatever dirX actually is as well.
    Something like this should give you results close to what you describe, just not in a co-routine:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MoveToTarget : MonoBehaviour
    4. {
    5.     public Transform Target;
    6.     public float MaxSpeed = 1.0f;
    7.     public float Threshold = 0.05f;
    8.  
    9.     private void Update()
    10.     {
    11.         var delta = Target.position - transform.position;
    12.         delta.z = 0.0f;
    13.  
    14.         if(Mathf.Abs(delta.x) > Threshold)
    15.         {
    16.             delta.y = 0.0f;
    17.         }
    18.         else if(Mathf.Abs(delta.y) > Threshold)
    19.         {
    20.             delta.x = 0.0f;
    21.         }
    22.         else
    23.         {
    24.             return;
    25.         }
    26.  
    27.         var move = (delta.normalized * MaxSpeed * Time.deltaTime);
    28.         if(move.sqrMagnitude > delta.sqrMagnitude)
    29.         {
    30.             move = delta;
    31.         }
    32.  
    33.         transform.position += move;
    34.     }
    35. }
    The object will first move towards a target along the x-axis up to a given threshold and at a maximum speed without overshooting, then do the same along the y-axis. Once both axes are at the threshold, it should stay put.
    Hope that helps!
     
  5. TimBorquez

    TimBorquez

    Joined:
    Mar 18, 2013
    Posts:
    81
    I'll look into this. Thank you for such a detailed reply! :D
     
  6. Deleted User

    Deleted User

    Guest

    Although it's an quite old post... I've came with a solution that works pretty well for me:
    I just check the distance points among the Vectors whom represents the current generated mesh.

    in my case it was Vector2 (because it's comparing against two cursor positions...)

    Code (CSharp):
    1.  
    2. private const float minAcceptableVertexDistance = 0.05f;
    3.          
    4. if (Mathf.Abs(initialPosition.x - finalPosition.x) < minAcceptableVertexDistance ||
    5.     Mathf.Abs(initialPosition.y - finalPosition.y) < minAcceptableVertexDistance)
    6. {
    7.     return false;   // Returns out of the function if the points are too close from each other. (Or simply "return")
    8. }
    9.  
     
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,924
    [QUOTE="erickluiss, post: 6621754, member: 4477513"I've came with a solution that works pretty well for me[/QUOTE]

    That checks whether you're within a square area (or a rectangle if the x,y #'s are different). If you're moving a square around other squares, which can't rotate, it works just fine to be sure no sides or corners can get too close. But otherwise it's clearly a square, and distance-from is a circle. Players will quickly notice that.
     
  8. That1phillps

    That1phillps

    Joined:
    May 16, 2018
    Posts:
    10
    If you are like me and were looking at this thread for the difference between a target point on the y axis of a rotation and another y value of rotation, you can just use: FloatA - FloatB. This way you still get negative values if you need them.