- Home /
Making a lineRenderer end 10 units in front of a gameObject.
Hey guys. So I'm making a stealth game right now, and the way that the enemy functions right now works as such:
The enemy patrols an area, and constantly shoots a raycast in front of them. If the raycast touches the Player, it checks to see if the player is 10 units or close to them by using Vector3.Distance.
To Make it fair, the enemy has a lineRenderer being drawn starting on them, and ending on where the raycast hits. This way the player knows when they are in sight of the enemy. What I can't figure out how to do though, is make it so the lineRenderer always ends 10 units in front of the enemy.
Answer by robertbu · Dec 12, 2013 at 01:23 AM
The world space position 10 units in front of an enemy would be:
enemy.position + enemy.forward * 10.0;
If the code is on the enemy, then you can use:
transform.position + transform.forward * 10.0;
These assume 1) that you have useWorldSpace checked in the line renderer, and 2) the front of your enemy is the positive 'z' side.
What I would do is check the raycast first and then set the position if the raycast fails. Pseudo-code:
var endPos : Vector3;
if (Physics.Raycast(...)) {
endPos = hit.point;
}
else {
endPos = transform.position + transform.forward * 10.0;
}
lineRenderer.SetPosition(1, endPos);