Firing arrows at trees, accuracy problems with Vector3.reflect and raycasts

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:

/WebPlayer.html

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” :smile:)

My guess is that getting your arrow to follow the predicted path exactly using the methods you’re using now will be an uphill battle (due to floating-point error). If you really need the arrow to follow the predicted path, your best bet might be simply to store the path, and have the arrow follow the precomputed path rather than simulate it using normal means. With a little tweaking I imagine the behavior would be more or less indistinguishable from what you have now, and you’d be able to guarantee that the path would be followed precisely, more or less.

Hehe, I’m glad I’m not the only one who noticed that :slight_smile:

Thanks for the reply. I did think of that method, storing the hit.normal vectors that the raycast’s find and then letting the arrows follow. But then they have to revert back to this method to keep moving, else they would just stop at the last stored vector. Seems like alot of work for little payoff.

The arrow behaviour seems fine until the 3rd bounce, I think you’re right, floating point error times every reflection increases the error range.

Instead I have just removed one of the trajectory raycasts, less cpu overhead and more challenge for the player. :slight_smile: