- Home /
Question by
Krazysh01 · Jul 09, 2014 at 09:38 AM ·
c#gameobjectlineclosest
How to draw a line from the player to the nearest object?
I'm trying to draw a line from the player to the nearest object of a group of objects. I have the following code but for some reason it doesn't draw the line. LineRenderer line;
void Start(){
line = gameObject.GetComponent<LineRenderer>();
}
void Update(){
Collider[] hitColliders = Physics.OverlapSphere(transform.position, 1000, 11);
float diff = Vector3.Distance (hitColliders [0].transform.position, transform.position);
int closest = 0;
for (int x = 1; x < hitColliders.Length; x++) {
if(diff > Vector3.Distance (hitColliders [x].transform.position, transform.position)){
closest = x;
}
}
line.SetPosition(0, transform.position);
line.SetPosition (1, hitColliders [closest].transform.position);
}
there are no errors in the code according to unity but any help you guys can provide I'll be extremely thankful for.
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# Access Script in Closest Object 1 Answer
How do I find the closest target with a tag? c# 2 Answers
Distribute terrain in zones 3 Answers