- Home /
AI Player Jerky Movement - Overcoming Obstacle
I was working in the 3D war game. So I have written code for the AI player for movement, rotation, overcoming obstacle and shooting.
When an AI player requires to overcome from the midway obstacle, it is doing continuous jerk in the movement. Jerks mean it is doing continuous left-right rotation and very quickly.
The following image represents the overall situation that currently exists:
AI Player is moving continuously from one place to another and try to find a way from placed obstacles within the environment. So AI player always has one target to reach so when it detects any obstacle it just does some rotation and then after again started moving ahead with already decided target.
It does not have any problem with bypassing the obstacle but its movement is not smooth that is the main problem.
Here is the complete code that I am using currently:
private void FixedUpdate()
{
if (StopMovement)
return;
myRigidbody.MovePosition(myRigidbody.position + transform.TransformDirection(aiMovement) * movementSpeed * Time.fixedDeltaTime);
TakeTurn();
}
private void TakeTurn()
{
Ray straightRightRay = new Ray(transform.position + Vector3.up * 0.5f + transform.right * 2f, transform.forward);
Ray straightLeftRay = new Ray(transform.position + Vector3.up * 0.5f - transform.right * 2f, transform.forward);
Debug.DrawRay(straightRightRay.origin, straightRightRay.direction * detectDistance, Color.green);
Debug.DrawRay(straightLeftRay.origin, straightLeftRay.direction * detectDistance, Color.green);
RaycastHit hit;
// If we detect an obstacle on our right side, swerve to the left
if (Physics.Raycast(straightRightRay, out hit, detectDistance, obstaclesLayerMask))
{
// Rotate left to avoid obstacle
Rotate(-1);
}
else if (Physics.Raycast(straightLeftRay, out hit, detectDistance, obstaclesLayerMask))
{
// Otherwise, if we detect an obstacle on our left side, swerve to the right
// Rotate right to avoid obstacle
Rotate(1);
}
else
{
// Rotate the car until it reaches the desired chase angle from either side of the player
if (Vector3.Angle(transform.forward, targetPoint.position - transform.position) > chaseAngle)
{
followAngle = ChaseAngle(transform.forward, targetPoint.position - transform.position, Vector3.up);
Rotate(followAngle);
}
else
{
// Otherwise, stop rotating
Rotate(0);
}
}
}
// Rotates the car either left or right, and applies the relevant lean and drift effects
public void Rotate(float rotateDirection)
{
// If the car is rotating either left or right, make it drift and lean in the direction its rotating
if (rotateDirection != 0)
{
// Rotate the car based on the control direction
myRigidbody.rotation = myRigidbody.rotation * Quaternion.Euler(Vector3.up * rotateDirection * rotationSpeed);
currentRotation += rotateDirection * rotationSpeed * Time.deltaTime;
if (currentRotation > 360)
currentRotation -= 360;
}
else
{
// Otherwise, if we are no longer rotating, straighten up the car and front wheels
// Return the base of the car to its 0 angle
myRigidbody.rotation = myRigidbody.rotation * Quaternion.Euler(Vector3.up * rotateDirection * rotationSpeed);
}
}
// Calculates the approach angle of an object towrads another object
public float ChaseAngle(Vector3 forward, Vector3 targetDirection, Vector3 up)
{
// Calculate the approach angle
approachAngle = Vector3.Dot(Vector3.Cross(up, forward), targetDirection);
// If the angle is higher than 0, we approach from the left ( so we must rotate right )
if (approachAngle > 0f)
{
return 1f;
}
else if (approachAngle < 0f)
{
//Otherwise, if the angle is lower than 0, we approach from the right ( so we must rotate left )
return -1f;
}
else
{
// Otherwise, we are within the angle range so we don't need to rotate
return 0f;
}
}
Please give me your side suggestion to solve this problem. If you want video recording for jerky player movement then let me know, I will record a short video for you.
Your answer
Follow this Question
Related Questions
Car AI Avoiding Obstacle 0 Answers
AI create path, help. 1 Answer
help with AI avoidance script 0 Answers
Physics.Raycast AI problem 1 Answer
Physics.Raycast 2 Answers