- Home /
Raycasting, How to draw a line to the position of the rays end when nothing is hit
So the problem I've got is that I need to be able to draw a Line using line renderer to the end of the raycast when nothing is hit. Here is a code sample that might help showing what I'm looking for:
Vector3 fwd = AttackPos.transform.forward;
if(Physics.Raycast(AttackPos.transform.position, fwd, out hit, shotDistance)){
if(hit.transform.tag == "Other"){
hit.transform.GetComponent<HealthManager>().TookDamage(bulletDamage, hit.point);
if(hit.transform.gameObject.layer == 9){
_netMan.ShotPlayer(hit.transform.GetComponent<Interpolation>().gottenIndex, bulletDamage, hit.point);
}
else{
if(hit.transform.GetComponent<Interpolation>() != null){
_netMan.ShotAI(hit.transform.GetComponent<Interpolation>().gottenIndex, bulletDamage, hit.point);
}
else{
_netMan.ShotAI(_netMan.playerINDEX, bulletDamage, hit.point);
}
}
}
if(hasLaserBeam){
DrawLine(linePos.transform.position, hit.point);
}
}
else{
if(hasLaserBeam){
DrawLine(linePos.transform.position, fwd*shotDistance);
}
}
All the code inside the if(hit) statement works, it is tThe else statement ( when nothing is hit) that is the problem. It keeps drawing a line towards the origo of the scene instead of to the position fwd*shotDistance. The attackposition is a child object so can this problem be related to transformpoint/transformdirection since the coordinates aren't in worldspace?
Answer by robertbu · Feb 10, 2014 at 07:17 PM
The problem is that your DrawLine() takes two positions (assumed from the first call to DrawLine()). On line 26, you are passing it a position and a direction. The line should be:
DrawLine(linePos.transform.position, AttackPos.transform.position+fwd*shotDistance);
This is correct.. That's what I wanted to reply.. I can't plus one this.. low reputation .. SOrry..
Silly me, of course. I knew I was overlooking something but just couldn't figure out what. Thanks for the quick and good response :)!
Your answer
Follow this Question
Related Questions
Drawing a line in front of player weapon 0 Answers
How can I can I cast a ray from a gameobject? 1 Answer
Applying Constant Force to Object A using Object B's Vectors. 0 Answers
Using raycast and collider to increase int 1 Answer
NullReference when checking tag via Raycast. How to solve this? 1 Answer