- Home /
Line Renderer bounce
I currently have a "laser sight" on my player character using a Line Renderer and a raycast. The player character shoots balls that bounce off of walls. I am trying to achieve an effect with my laser sight that you see in most Pool games- a line that works like a laser sight, but when the end of the line comes in contact with the sides of the pool table, an angle of ricochet is displayed letting the player know were the ball is going to bounce. The picture below shows exactly what I am trying to achieve:
EDIT
I am having a bit of trouble with the implementation. The line does not seem to be reflecting or bouncing, instead they seem to be overlapped on top of each other. See code:
var hit : RaycastHit;
// these are set in start private var line : LineRenderer;
private var point01 = Vector3(0,0,0);
function Start () {
line = this.gameObject.AddComponent(LineRenderer);
}
function Update () {
line.SetPosition(0, point01); Physics.Raycast(transform.position,transform.forward,hit); if(hit.collider) { line.SetPosition(1,Vector3(0,0,hit.distance));
line.SetPosition(2, Vector3.Reflect(Vector3(0, 0, hit.distance), Vector3.right));
}
else
{
line.SetPosition(1,Vector3(0,0,5000));
}
Answer by Bunny83 · Feb 28, 2011 at 08:12 PM
I guess this two things should be enough : Vector3.Reflect and RaycastHit.normal. If you want to bounce multiple times you have to raycast again for every bounce. But the accuracy is getting worse each bounce.
edit
Another problem is that your ball is not infinite small like a raycast. That means your ball is touching the wall a bit earlier. You can use Physics.SphereCast() but if your ball/projectile have not a spherical form it's getting difficult.
Great answer Bunny. I was thinking about the accuracy thing, I think this would work: the part of the ball that interacts with the side is always exactly the radius of the sphere away from the center of the ball. The center will follow the same line though, but it'll hit 'earlier'. To solve this, create invisible walls away from the real walls exactly r more inside the field. Now if you make your raycast bounce of this it should show the exact trajectory of the center of your ball.
Your answer
Follow this Question
Related Questions
How can i shoot 2D ball/bounce of wall using Raycast? 1 Answer
LineRenderer missing in build 1 Answer
Can someone help me with this line renderer? 0 Answers
Draw a line with collision 2 Answers
Making a border on a LineRenderer 0 Answers