Creating X points in an oval around a Vector3. Help with MATH! :S

Man I suck at math and my stumbling has brought me this close…
I have a character with 2 feet, crazy right.
I want to create 12 points in an oval around each foot. This is what I have on Play.
8703816--1175136--upload_2023-1-4_20-13-37.png

This is perfect on load. Unfortunately it only coincidentally works because he is facing the right way. See below…
8703816--1175142--upload_2023-1-4_20-14-52.png

First here is my code…

public void getPoints() {
        points.Clear();
        float centerX = transform.position.x;
        float centerZ = transform.position.z;

        // Loop through each angle and calculate the x and y coordinates of the point at that angle
        for (float angle = 0; angle < 360; angle += 360.0f / pointCount)
        {
            float x = centerX + maxStepSideDistance * Mathf.Cos(angle * Mathf.Deg2Rad);
            float z = centerZ + maxStepForwardDistance * Mathf.Clamp(Mathf.Sin(angle * Mathf.Deg2Rad), -1.0f, 1.0f);

            // Create a Vector3 object for the point and add it to the list
            Vector3 point = new Vector3(x, 0, z);

            points.Add(point);
        }
    }

Is there a simple way (Or complex) that I can do to make this point in the direction the character is facing, like the first pic?

I suck at math and cos and all that so hoping it is more simple like using local coordinates or something :S
Thanks

Exactly calculate the local oval position and then use transform to convert it to global pos. Something like this:

public void getPoints() {
        points.Clear();
 
        // Loop through each angle and calculate the x and y coordinates of the point at that angle
        for (float angle = 0; angle < 360; angle += 360.0f / pointCount)
        {
            float x = maxStepSideDistance * Mathf.Cos(angle * Mathf.Deg2Rad);
            float z = maxStepForwardDistance * Mathf.Sin(angle * Mathf.Deg2Rad); // Useless clamp, value of sin can't be outside the [-1, 1] range. No point clamping it.
            Vector3 point = new Vector3(x, 0, z);
            point = transform.TransformPoint(point);
            points.Add(point);
        }
}

Or even

public void getPoints() {
        points.Clear();
 
        // Loop through each angle and calculate the x and y coordinates of the point at that angle
        var size = new Vector3(maxStepSideDistance, 0, maxStepForwardDistance);
        for (float angle = 0; angle < 360 * Mathf.Rad2Deg; angle += 360.0f * Mathf.Rad2Deg / pointCount)
        {
            var circlePos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle));
            var ovalPoint = Vector3.Scale(size, circlePos);         // obtain oval by stretching circle
            var globalPoint = transform.TransformPoint(ovalPoint);
 
            points.Add(globalPoint);
        }
}

My recommendations for getting better at vector math is think about it as sequence of stops and do them in sequence instead of trying to put everything into single expression. That way you can more easily reorder or add something in between later.

Also very rarely do you need to manipulate each individual component most of the time you will be doing same kind of calculations on all the vector components. Copying expressions for each component just makes the whole thing look more complex than it actually is. Thinking in terms of vectors also helps when the components start to mix like after applying rotations.

1 Like