Hi,

I have objects that will only move in a straight line and will ricochet off of any surface that they hit.

I’m trying to calculate the angle at which to launch the object, taking ricochet’s into account, that will result in the shortest path taken to reach a target.

I’m using the following line of code to apply force to the object

rigidbody.AddForce(GetLaunchDir() * 500);

```
Vector3 GetLaunchDir()
{
Vector3 bestDir = new Vector3(0,0,0); // this is used to store the best direction to launch this rigidbody
collider.enabled = false; // I turn off the collider on this object so it isn't caught by my sphere casting
for(float deg = 0; deg < 360; deg += 1) // I'm checking every euler angle between 0 and 360
{
Vector3 dir = (Quaternion.Euler(0,deg,0) * Vector3.forward).normalized; // this sets the starting angle to be checked this loop
Vector3 start = transform.position; // start out at enemy's current position
Vector3 launchDir = dir; //store the starting angle for this check, not to be changed
float distance = -Mathf.Infinity; // store the shortest distance found
float travelDist = 1000; // this is the maximum distance that will be checked
for(float dist = 0; dist < travelDist; dist += 0) // for each angle, calculate ricochets so long as the distance 'traveled' by the check hasn't exceeded travelDist, i have travelDist decrement within the loop rather than dist increment
{
RaycastHit tempHit; // store raycast hit from spherecast this loop
if(Physics.SphereCast(start,objRad,dir, out tempHit, travelDist)) // get the next object this enemy will hit if traveling in direction dir from position start
// objRad is the object's collider radius
{
travelDist -= Vector3.Distance(start, tempHit.point); //decrement travelDist by the distance of this check
if(tempHit.transform.gameObject == target.gameObject) //if the hit object is the target, see if this is the best direction so far
{
if(travelDist > distance) //if travelDist is greater than distance, then this is currently the shortest path
{
bestDir = launchDir;// set bestDir to the initial launchDir
distance = travelDist;//set distance to travelDist as this is the new best to beast
}
travelDist = 0;
}
else // if this hit an object other than the target
{
start = tempHit.point; // set start to the currently hit position so the next check starts from there
dir = Vector3.Reflect(dir,tempHit.normal).normalized; // reflect dir off of the normal of raycast hit
}
}
else // if the spherecast didn't hit anything, set travelDist to 0 to cancel this angle's check
{
travelDist = 0;
}
}
}
collider.enabled = true; //after all checks, set this object's collider back on
return bestDir; //return the bestDir found to be used for the force
}
```

This is giving me unexpected and unwanted behavior, launching the object in odd directions. What am I doing wrong?

I am searching for a solution and have yet to find one.

Thanks for your time!