Search code examples
c#unity-game-enginetrigonometry

Calculating shoot angle


My player throwing 5 knives to ground with different angles.

        else if (throwType == "onGround")
        {
            float angle = 15f;
            for (int i = 0; i < 5; i++)
            {
                Instantiate(kunai, attackPoint.position, transform.rotation * Quaternion.Euler(0f, 180f, angle));
                angle += 10f;
            }
        }

I tryed this code but I want the distance between each of the stabbing points of all the blades to be equal on level ground. Here is the triangle example that I meant. What method or solution do I use?


Solution

    1. Distribute your target points evenly along the ground, or in any other pattern.
    2. Calculate the direction to each point, i.e. point - attackPoint.Position
    3. Use Vector3.Angle(Vector3 from, Vector3 to) to calculate the angle between each direction and your reference, presumably Vector3.down.

    In general I would recommend staying away from angles when possible, and instead use methods like Quaternion.LookAt to create rotations from direction vectors. I find vectors much easier to reason about and visualize, but you will need at least some familiarity with vector algebra.