Hi,
trying to make bouncing laser. I found few examples using Vector3.Reflect, but cant get it work like i thought it would. I think there is something i cant understand or see anymore, or perhaps Reflect is not ment to be used like this:
function Update ()
{
drawLaser(transform.position,3);
}
function drawLaser(startPoint:Vector3,n:int)
{
var hit : RaycastHit;
var rayDir:Vector3 = transform.TransformDirection (Vector3.forward);
for(var i = 0; i < n; i++)
{
if (Physics.Raycast (startPoint, rayDir, hit, 1000))
{
Debug.DrawLine (startPoint, hit.point);
rayDir = Vector3.Reflect(startPoint, hit.normal) ;
startPoint = hit.point;
}
}
}
And here is a picture (white line by code, green one from my mind):
The Vector3.Reflect doesn’t give you a direction, but a position at the opposite of the orginal vector. So the function is more like a “Set position” for a mirror effect rather than making a laser bounce of a wall.
You have to make your own bounce calculation, which shouldn’t be too hard.
For instance:
If the laser bounces of something that is not a floor, then switch the x and/or z direction to the opposite. If the laser hits something that is a floor, change the y direction (if you would want to).
Good luck!
You almost made me believe this cant be done with Reflect :) Thanks for your opinion anyway, made me to reread docs and couple answers. But yes you are right about opposite of the original vector, i had somekind of misunderstanding there.
You almost made me believe this cant be done with Reflect :) Thanks for your opinion anyway, made me to reread docs and couple answers. But yes you are right about opposite of the original vector, i had somekind of misunderstanding there.
– chelnokHehe, I never said it couldn't be done with reflect ;-) But good that it works and a thumbs up that you put in the code for future reference!
– CodeMasterMike