Search Unity

Question cannot get accurate direction between 2 points?

Discussion in 'Scripting' started by yuriythebest, Nov 19, 2022.

  1. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    1,125
    Hello! I'm facing a weird issue. When I raycast between 2 points (or, draw a line for debugging between these 2 points), the raycast "misses" the target. it's really weird. Somehow, my "Direction" is wrong?

    Code (CSharp):
    1.    
    2. Vector3 targetPos = currentTarget.transform.position;
    3. targetPos.y= Mathf.Clamp(targetPos.y,0,2.9f); //so as not to shoot at the "feet" but the middle of the enemy. without it same thing
    4. direction = ( targetPos - rayStartPos ).normalized;
    5. Vector3 destination  = direction*500;
    6. destination.y = Mathf.Clamp(destination.y,0,1.9f);
    7. ray       = new Ray( rayStartPos, direction );
    8. Debug.DrawLine  (rayStartPos,destination,Color.magenta);
    raycasts.png

    the raycasts are consistent/ do not jump around between frames so it's not a floating point thing?
    thanks in advance!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    what do you mean by "misses"? Define what you expect, versus what is happening that counts as "missing".

    For example... "I perform a physics raycast against the meshcollider of the enemy and I expect to get back the point on the mesh that effectively is where my gun is pointing... but my raycast call returns false (no collision)."

    ...

    Next... you draw a debug line using Debug.DrawLine between rayStartPos and destination. What is the purpose of this?

    Are you expecting this to show you where the raycast is? Because it won't. You've arbitrarily clamped destination's y value between 0 and 1.9. Why? Depending on the angle of approach between rayStartPos and targPos (which is also arbitrarily moved to 2.9 in the y) could result in wildly different lines being drawn from the actual ray. So the debug line serves no real purpose.

    And as for that adjusting to 2.9. Why 2.9? Is your world map going to always be just a giant flat plane? Are all enemies 6 units tall (you say midpoint... so 2.9 * 2 is just shy of 6)? What if you have short enemies?

    Speaking of... where is your terrain height set at anyways? If you move that plane up or down this code will break. Is that possibly the cause?
     
    Bunny83 likes this.
  3. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    1,125
    Hi lordofduct! Ok, for clarity I uploaded a screenshot where I removed the clamp from the code so as not to simplify as much as possible for now.
    What I'd expect: the raycasts/lines should go to currentTarget.transform.position. Instead, they are slightly off. I double checked to make sure that this was indeed the enemy that was targeted.

    Code (CSharp):
    1. Vector3 targetPos = currentTarget.transform.position;
    2. //REMOVED targetPos.y= Mathf.Clamp(targetPos.y,0,2.9f); //so as not to shoot at the "feet" but the middle of the enemy. without it same thing
    3. direction = ( targetPos - rayStartPos ).normalized;
    4. Vector3 destination  = direction*500;
    5. ray       = new Ray( rayStartPos, direction );
    6. Debug.DrawLine  (rayStartPos,destination,Color.magenta);
    screen22.png
     
  4. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    You don’t need to normalise.
    You may also need the negative of your direction.
     
    yuriythebest likes this.
  5. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    1,125
    Yess AnimalMan! You are awesome - the culprit was indexed the normalization. My guess would be that it removes some floating point values. Without it works great
    Code (CSharp):
    1.  
    2. direction = ( targetPos - rayStartPos );
     
    AnimalMan likes this.