- Home /
Line rendering from transform.position to transform.rotation * Vector2.right * float doesn't work completely.
Hi!
I just started programming a new enemy that is supposed to stand still and just point towards the player (more to be added later). But this simple code doesn't make the line point exactly where the enemy is heading even though it should as far as I can see.
Here's a video of the problem (and yes the issue is visible without the zoom): https://i.imgur.com/ukRvBfF.mp4
And here's the code:
private void Aim()
{
Vector2 targetDir = (target.position - transform.position);
float zAngle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(new Vector3(0, 0, zAngle)), rotSpeed * Time.deltaTime);
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, transform.rotation * Vector3.right * range);
}
why are you ai$$anonymous$$g before calculating the final rotation for the frame?
I was trying out different things to understand why the object behaved the way it died so I just messed around with the order of the code. That is just a remainder of me experimenting. The order doesn't have any notable influence on the behavior of the enemy but I will correct it since it would be illogical to keep it that way.
it doesn't make sense that this code alone would be responsible for the small offset we're seeing.
is any angularforce interfering or is some other code also calculating rotation?
Answer by Eno-Khaon · Dec 23, 2018 at 07:53 PM
No matter what the unit's rotation is, your line will currently go from the unit's position to world position (0, 0) plus a normalized 1-unit offset * range.
You need to add a base position to apply that offset to, and that position is the same as your first one.
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, transform.position + transform.rotation * Vector3.right * range);
Ah yes, thank you! Why didn't I think of that? I managed to work around the solution by using local space ins$$anonymous$$d of world space but I will use your solution ins$$anonymous$$d since it is more practical for the rest of my code.
Your answer
Follow this Question
Related Questions
How to restrict rotation 2 Answers
Rotate object around object with fixed value. 1 Answer
How Do I Give Two Objects the Same Transform? 1 Answer
Objects not rotating right half the time based on another object 0 Answers
Distribute terrain in zones 3 Answers