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. Dismiss Notice

Working with directions?

Discussion in 'Scripting' started by SolitaryGaming, Jan 10, 2021.

  1. SolitaryGaming

    SolitaryGaming

    Joined:
    Nov 21, 2020
    Posts:
    10
    Hi

    I'm having a very hard time finding resources to help me learn about working with directions as Vectors. I know that Up or North is (0,1), and Northwest would be (0.5,0.5), but I can't find any resources on what sort of math would be needed to work with any sort of directions other than this (Say halfway between northwest and west.)

    What I'm trying to do is create a context map for my AI's decision making. I want to be able to divide the world up into a set amount of directions (in relation to where the enemy is,) and have it make decisions based on which direction seems the most desirable to achieve it's current goal. I've read all sorts of papers and other resources on Context Steering (the exact thing I'm trying to implement,) but I've never been very math smart and can't wrap my head around the division of the directions.

    Ideally what I'd achieve is a function that I could call that would return a list of Vector2 directions, and would take an input for the amount of directions I want to turn the world into (So if I called it for 4 directions it would return Vector2.up, vector2.down, etc.)
    The reason I specifically want to be able to choose the amount of directions through a function, rather than do the math myself and hardcode it in ahead of time, is for performance reasons. Just having 4 or 8 directions, while easy, looks very rigid when I lock my steering behaviors to them. I want to be able to tweak to see exactly how much I can get away with, and have certain enemies have more circular movement than others.

    I don't need somebody to write the function for me per se, but math was never my strong suit, and I've only ever needed to alter already determined directions before (steer away from an obstacle, etc.) I have no idea what the field of math is even called, and when I try to google it I don't find what I'm looking for because I don't have the right words apparently. Any help even finding the resources to understand this would be much appreciated!

    I've tried searching up Vector Directions, c# dividing circles, etc with no avail so I'm really hoping somebody here can at least point me in the right direction.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I think this video and the related playlist could help you understand



    this could also help
     
    SolitaryGaming and Bunny83 like this.
  3. SolitaryGaming

    SolitaryGaming

    Joined:
    Nov 21, 2020
    Posts:
    10
    That helped a lot thank you very much. In the interest of if it helps anybody else, I'll share the code that I wound up coming up with to solve my question here:

    Code (CSharp):
    1.         private List<Vector3> GetDirections(int amountOfDirections)
    2.         {
    3.             List<Vector3> generatedDirections = new List<Vector3>();
    4.             float angleStep = ((ushort)(360.0 / amountOfDirections));
    5.  
    6.             for (int i = 0; i < amountOfDirections; i++)
    7.             {
    8.                 float theta = i * angleStep;
    9.                 float newX = Mathf.Cos(theta * Mathf.Deg2Rad);
    10.                 float newY = Mathf.Sin(theta * Mathf.Deg2Rad);
    11.                 generatedDirections.Add(new Vector3(newX, newY, 0));
    12.             }
    13.             return generatedDirections;
    14.         }
    I really appreciate you taking the time to answer this. I'm sorry for the late reply, it wound up taking me way too long to write this simple function as I have apparently forgotten all of maths since leaving school.
     
    SparrowGS likes this.