I am trying to implement the arrive steering behavior. The thinking behind is my game entity should move towards a target at a certain speed. when the entity is within a certain distance away from the target it should decelerate until it stops at its target. I tried to implement this but something crazy happens. If i use a radius like of magnitude 5 my game object just moves past its target and continues to do so. If I select a larger radius like 50 or hundred my object decelerates and come to a stop but it will be before it reaches its target. Here is my code of what I am trying to do
/**
* This behavior is similar to seek but it attempts to arrive at the
* target with a zero velocity
*/
public Vector3 Arrive(){
//find the required velocity to reach the target
direction = target - m_pPlayerPos;
direction.y = 0;
//find the distance between object and target
float distance = direction.magnitude;
//value to hold the deceleration value
float deceleration;
//value to hold the required steering force
Vector3 steeringForce;
//check to see whether the character
//is inside the slowing area
if(distance < slowingRadius)
{
//inside the slowing area(use newton's equation v^2 = u^2 + 2as)
deceleration = (0 - (moveSpeed * moveSpeed))/ (2 * slowingRadius);
moveSpeed += deceleration;
steeringForce = direction.normalized * Time.deltaTime * moveSpeed;
}else{
//outside the slowing area
steeringForce = direction.normalized * Time.deltaTime * moveSpeed;
}
return steeringForce;
}
Here is where the Arrive is called by the game object
//instance of the steering behaviours
public SteeringBehaviours m_pSteering;
//variable to store the ball
protected GameObject m_pBall;
// Use this for initialization
public void Start(){
m_pSteering = new SteeringBehaviours(new Vector3(transform.position.x,transform.position.y,transform.position.z));
m_pBall = GameObject.FindGameObjectWithTag("soccerBall");
}
// Update is called once per frame
void Update () {
m_pSteering.SetTarget(m_pBall.transform.position);
//FaceBall();
transform.position += m_pSteering.Arrive();
}