Search Unity

simple syntax error, distance <= attackrange

Discussion in 'Scripting' started by NatalieBaldwin, Feb 22, 2017.

  1. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    My transform.position - their transform.position <= float attackRange; It says i can't use <= to compare vector3 with float. How do i make it work? I just want to calculate the distance between me and my enemy and compare to my variable to see if it's in range.
     
  2. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    One vector minus another results in a third vector. This vector is indeed related to distance, but it's a multi-valued quantity, just like any other vector, so it can't be directly compared to a single number. The result of the subtraction is essentially the arrow that is draw between the two points, so it also contains the direction from the second point to the first.

    The length is all you want, so you can either do (transform.position - other.transform.position).Length <= attackRange, or you can use the static function made precisely for this purpose: Vector3.Distance(transform.position, other.transform.position) <= attackRange.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Vector3 is a structure of 3 floats, 3 dimensional, it represents 3d direction and magnitude:
    <x,y,z>

    A float is a scalar, 1 dimensional, it represents 1d scale or magntidue:
    x

    To compare a Vector3 and a float doesn't make sense. That'd be like saying:

    "Is 1 mile east, 2 miles north, and 1000 feet elevation < 1 mile"

    Is WHAT less than 1 mile? The total distance traveled? The easterly distance traveled? The elevation traveled?

    Nevermind that you didn't even use a direction vector. You used an orientational vector. A 'position'.... so you really asked "is the location of New York City <= 1 mile"? This logically doesn't make sense. Do you mean "is the distance to New York City <= 1 mile"... well we need more info than... we need to get the distance from point X to New York City. For such a thing you might use Vector3.Distance:
    https://docs.unity3d.com/ScriptReference/Vector3.Distance.html
     
  4. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    I knew why it wasn't working just didn't know how to fix it. I used vector3.distance(them and me) <= attackrange thank you. I actually thought i had tried that exact thing but i guess not.... I have a follow-up question that's a little more complicated though. I don't want the Y distance to come into play in my game almost at all. I want the distance to be calculated solely on x and z. Is there an easy way to do that?
     
  5. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    I'd personally just write my own helper function that takes two Vector3s, sets the y value to zero on both of them, and then returns the result of Vector3.Distance(). Call it FlatDistance() or something. If I was really particular on cutting down on math operations in the name of efficiency, I'd use the 2D version of the Pythagorean theorem and do the Sqrt(dx*dx, dz*dz) myself.

    Edit: If I was really aiming for optimal performance, I'd not even do the square root, and compare the squared distance with the pre-computed square of the attack range. If distance <= attackRange, then it's also true that distance^2 <= attackRange^2.
     
  6. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    That first part was the obvious solution i was planning but don't know how to do yet, the rest didn't make sense. I'll read it again in the morning and probably instantly understand. Thank you both.