Search code examples
c#unity-game-enginevectormath

Unity angle and position relative to direction


I'm trying to create a 'star' of vector points around a point with a constant angle between them and the original line between the source and the hit point (see pic) which I've done by creating new vectors with a small offset from the original:

enter image description here

private void FixedUpdate()
{
    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out hit, 3000))
    {

    Vector3 mousePos = hit.point;
    Debug.DrawLine(transform.position, hit.point, Color.yellow);

    Vector3[] explorePoints = new Vector3[6] {
                new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z + 1), // diag left
                new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z + 1), // diag right
                new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z), // left
                new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z), // right
                new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z - 1), // diag left back
                new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z - 1), // diag right back
    };

    for (int x = 0; x < explorePoints.Length; x++)
    {
        Debug.DrawLine(mousePos, explorePoints[x], Color.red);
    }

}
}

This works fine when the angle between the mouse approaches 0 or 180, but of course not at other angles:

enter image description here

I know I probably need the Quaternion class to apply the angle between the sphere and the mouse point to the direction vectors but can't quite figure it out e.g.

Quaternion q = Quaternion.FromToRotation(transform.position, mousePos);
for (int x = 0; x < explorePoints.Length; x++)
{
      Debug.DrawLine(mousePos, q * explorePoints[x], Color.red);
}

How do I keep the red lines at n angle to the yellow line at all times?


Solution

  • enter image description here

    private void FixedUpdate()
      {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 3000))
        {
          Vector3 mousePos = hit.point;
          Debug.DrawLine(transform.position, hit.point, Color.yellow);
          Vector3 rayDir = transform.position - mousePos;
          Vector3[] explorePoints = new Vector3[6] {
            Quaternion.Euler(0, 0, 45) * rayDir.normalized,
            Quaternion.Euler(0, 0, 90) * rayDir.normalized,
            Quaternion.Euler(0, 0, 135) * rayDir.normalized,
            Quaternion.Euler(0, 0, -45) * rayDir.normalized,
            Quaternion.Euler(0, 0, -90) * rayDir.normalized,
            Quaternion.Euler(0, 0, -135) * rayDir.normalized,
          };
    
          float starLength = 100;
          for (int x = 0; x < explorePoints.Length; x++)
          {
            // we want to use the vector as DIRECTION, not point, hence mousePos + explorePoints[x] (starLength is just the length of the red line)
            Debug.DrawLine(mousePos, mousePos + (explorePoints[x] * starLength), Color.red);
          }
        }
      }