Search Unity

How to check if Vector is opposite to another

Discussion in 'Scripting' started by Darkkingdom, Apr 5, 2019.

  1. Darkkingdom

    Darkkingdom

    Joined:
    Sep 2, 2014
    Posts:
    81
    Hey guys,

    I'm currently looking for way to negate the opposite input if my player is moving.
    So I need to check if one normalized vector is opposite to another normalized vector within an angle range.
    But I think a graph could better explain it:



    So I need to check if my Vector 2 (the player input) is within the red range regardless in which direction Vector 1 point towards.

    Has anyone a clue how to archive this?


    Thank you very much :)
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Ayill, SparrowGS, Darkkingdom and 2 others like this.
  3. Darkkingdom

    Darkkingdom

    Joined:
    Sep 2, 2014
    Posts:
    81
    Ahhhhh thank you so much hpjohn :)
     
  4. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    Also don't forget that a value of 0.5 is not equal to 45°, I see this done too often so I put the warning here. Basically get another placeholder vector at the threshold, and get the dot product and use that as a threshold value. I'm saying that because you can then create an interface to just tweak the place holder vector direction to fine tune the exclusion zone.