Hello.
First let me thank the community for all the invaluable help I have received in the past, just lurking these boards, the Unity answers and the Wiki community pages.
My problem is that I’m using lineRenderer to show where my arrows bounces will be and thus where the arrow will finally land. I’m doing this by shooting a ray, working out the reflect and then shooting another ray…
the arrow then fires and has its own little ray projecting from it, works the reflect in much the same way. But there must be small differences or errors in my code because the arrow doesn’t always follow the predicted course.
I was first using the physics engine but there were too many variables that influence the behaviour. The arrow has no collider, just a rigidbody.
I also found that using cylinderal colliders for the tree was making the discrepancy worse, at the moment the trees are using mesh colliders.
It seems that the discrepancy becomes greater the further away from the normal plane the angles are. Am I working out the initial direction for the Vector3.reflect correctly?
The arrows shoots out a raycast 0.1 long, could this be where the problem lies?
Here is a webplayer so you can see for yourself:
Here the two main scripts. One for the trajectory prediction:
lineRenderer.SetPosition(0, transform.position);
relative = transform.position + transform.TransformDirection(Vector3(0, 0, 5)); // changes local offset into a world offset
//Debug.DrawLine (transform.position,relative, Color.red);
if (Physics.Raycast(transform.position,transform.forward,hit,5)){;
sensitivemodifer1 = 3.9;
lineRenderer.SetPosition(1,hit.point);
lineRenderer.SetVertexCount(3);
reflect1 =(Vector3.Reflect(relative, hit.normal));
lineRenderer.SetPosition(2,reflect1);
if (Physics.Raycast(hit.point,reflect1,hit2)){;
sensitivemodifer2 = 1;
hit2.point.y=0.65;
lineRenderer.SetPosition(2,hit2.point);
lineRenderer.SetVertexCount(4);
reflect2 =(Vector3.Reflect(reflect1, hit2.normal));
lineRenderer.SetPosition(3,reflect2);
if (Physics.Raycast(hit2.point,reflect2,hit3)){;
lineRenderer.SetPosition(3,hit3.point);
}
}
else sensitivemodifer2 =0;
}
else{
lineRenderer.SetVertexCount(2);
lineRenderer.SetPosition(1,relative);
and here is the code running on the instanced arrow:
function Update () {
rigidbody.velocity = transform.TransformDirection (Vector3(0,0,2));
transform.LookAt(transform.position + rigidbody.velocity.normalized);
Debug.DrawLine (transform.position - Vector3(0,0,0.1),transform.position, Color.red);
var hit : RaycastHit;
Physics.Raycast(transform.position,transform.forward,hit,0.1);
if(hit.collider){
relative = lastpos + transform.TransformDirection(Vector3(0, 0, 5)); // changes local offset into a world offset
//relative.Normalize();
reflect = Vector3.Reflect(relative, hit.normal);
transform.LookAt(reflect);
lastpos = hit.normal;
}
}
Any help here would be greatly appreciated.
Thanks.
(I love the new forum design by the way, no more “please wait for your next search query” )