- Home /
How to add to implement the Arrive steering behavior
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();
}
Answer by Dave-Carlile · Dec 16, 2014 at 04:31 PM
I think the idea is to clamp your velocity by the remaining distance so it's reduced from the max down to zero as you approach...
if (distance < ArrivalDistance)
{
desiredVelocity = Vector2.ClampMagnitude(desiredVelocity * distance, MaxSpeed);
}
I have not yet tried your solution but here is what I tried again. The problem is my gameobject does not stop at its target. It stops a certain distance away from the target. Here is my code 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 = new Vector3();
//check to see whether the character
//inside the slowing area(use newton's equation v^2 = u^2 + 2as)
deceleration = (0 - (moveSpeed * moveSpeed))/ (2 * distance);
moveSpeed += (deceleration/distance);
return steeringForce = direction.normalized * Time.deltaTime * moveSpeed;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer
How to manupulate objects in 3d(Steering Behaviours) 0 Answers