1
\$\begingroup\$

I'm trying to fire three raycasts in the direction of movement, from offset starting points. I'm having trouble calculating the start positions of the rays based on the direction.

As you can see in the animation, the three parallel lines in the middle overlap when moving upwards using the code below, but I want them to remain evenly spaced.

Debug Rays

I cannot use transform.right (up/etc) etc because the transform is always facing the same direction by design. Instead I am using the currentDirection which is the normalized last movement velocity.

I want the lines to always be evenly spaced based on the direction the object is moving without rotating the actual transform - the problem is I don't know the math behind it and have spent last several hours trying to figure it out.

Current code:

Debug.DrawRay(this.transform.position, currentDirection * (avoidObjectsScanDistance + 0.05f), Color.blue, 0.09f);
Debug.DrawRay(this.transform.position + new Vector3(0, 0.1f, 0), currentDirection * (avoidObjectsScanDistance + 0.05f), Color.magenta, 0.09f);
Debug.DrawRay(this.transform.position + new Vector3(0, -0.1f, 0), currentDirection * (avoidObjectsScanDistance + 0.05f), Color.blue, 0.09f);

I've tried to get the position based on direction by using

var drawRayTopPos = currentDirection * localpos + position;
\$\endgroup\$
0
\$\begingroup\$

Answer:

  //find angle of direction
  float angle = Mathf.Atan2(currentDirection.y, currentDirection.x) * Mathf.Rad2Deg;
  //convert angle to rotation
  Quaternion rot = Quaternion.AngleAxis(angle, Vector3.forward);

  Debug.DrawRay(TransformPoint(transform.localPosition, 
    new Vector3(0,0.3f,0), rot, 1), 
    Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right,
    Color.magenta, 
    0.1f);

  Debug.DrawRay(TransformPoint(transform.localPosition, 
    new Vector3(0, -0.3f, 0), rot, 1), 
    Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right, 
    Color.magenta, 
    0.1f);

//convert local to world position including rotation
Vector2 TransformPoint(Vector3 referencePoint, Vector3 p, Quaternion rot, 
float scale)
{
    return referencePoint + ((rot * p) * scale);
}
\$\endgroup\$
0
\$\begingroup\$

Since it looks like you're working in the 2D x/y plane here, you can use a cute little trick:

Vector3 perp = new Vector3(-currentDirection.y, currentDirection.x, 0);

This gives you a vector perpendicular to currentDirection (rotated 90 degrees about the z axis) without any trigonometric functions or quaternions required. :)

Now your three offsets can be:

Vector3 raySpan = currentDirection * (avoidObjectsScanDistance + 0.05f);

Debug.DrawRay(this.transform.position, 
        raySpan, Color.blue, 0.09f);
Debug.DrawRay(this.transform.position + 0.1f * perp, 
        raySpan, Color.blue, 0.09f);
Debug.DrawRay(this.transform.position - 0.1f * perp, 
        raySpan, Color.blue, 0.09f);

Nice and compact. :)

In more general contexts, you can use the Quaternion.LookRotation method to form a quaternion that aligns forward in a particular direction, without needing to explicitly compute the angle yourself.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.