Advertisement

[Unity] Draw Angle Ray

Started by August 11, 2020 06:48 PM
6 comments, last by behdadsoft 4 years, 3 months ago

Hello.

For draw ray with angle at right and left of my sprite, I using code below.

        Vector3 center = transform.position + -transform.up;
        Debug.DrawLine(transform.position, center, Color.cyan);

        Vector3 rightDir = Quaternion.AngleAxis(45.0f, transform.forward) * -transform.up;
        Debug.DrawLine(transform.position, rightDir, Color.cyan);

        Vector3 lefttDir = Quaternion.AngleAxis(-45.0f, transform.forward) * -transform.up;
        Debug.DrawLine(transform.position, lefttDir, Color.cyan);

But problem is when my sprite is not moving, drew ray seem correct but after moving, left and right rays are stick to center of screen. (Attached pictures)

when i used DrawRay instead of DrawLine in my code, all things seem are OK even after moving sprite. but affect of ray is exactly like (After Moving Picture).

After Moving
Before Moving

You need to alter rightDir and leftDir as well when moving because you never update them to “point to the direction in relation to the position of the sprite” rather than “point to the direction from the initial vector (0, 0, 0)".

DwarLine is absolute, not relative

Advertisement

I called this code in Update() method but doesn't work.

behdadsoft said:
Vector3 rightDir = Quaternion.AngleAxis(45.0f, transform.forward) * -transform.up; Debug.DrawLine(transform.position, rightDir, Color.cyan);

And you still don't alter the position of your vector. rightDir is always pointing the same location. DrawLine ist NOT relative rather than absolute in opposite to DrawRay, which starts at an origin and simply draws the ray into a direction. DrawLine draws, well a line between two points in 3D space

Thank you Shaarigan.

Do you know about https://answers.unity.com?

Advertisement

@taby I forgot that.

Thanks for the reminder.

This topic is closed to new replies.

Advertisement