How to create steering behaviours for a goal keeper
I am trying to set up a soccer game. I have managed to set up an A.I that plays by itself observing different rules of the game. My steering behaviors have worked well for my field players but not as I would have wanted for my goal keeper and other motions like back paddling or side movements. At the beginning I have designed my behaviors in a way such that a player simply rotates to face his direction and move in that way. Now I want to implement more complex movements like back paddling side movements etc. So I redesigned my behaviors in a way such that a player now has a steering target("where he moves to") and a track target("where he always looks at ") e.g a player might decide to move to a certain home region behind him whilst looking at the opponents's goal or ball. So I tried to implement this design but it does not work, removing the code that rotates the player results in my player only moving forward along the Z-axis. And if I add the code for the player to rotate and look at its target that code is being overridden somehow and the player only faces its steering target and not the track target. Here is my seek behavior before adding the track target code
direction = steeringTarget - m_pPlayer.transform.position;
direction.y = 0;
direction = direction.normalized;
if (directionToTrackTarget != Vector3.zero) {
m_pPlayer.transform.rotation = Quaternion.Slerp (m_pPlayer.transform.rotation, Quaternion.LookRotation (direction), Parameters.MaxRotationSpeed * Time.deltaTime);
m_pPlayer.transform.eulerAngles = new Vector3 (0, m_pPlayer.transform.eulerAngles.y, 0);
}
Vector3 forward = m_pPlayer.transform.TransformDirection(Vector3.forward);
float speedModifier = Vector3.Dot(forward, directionToTarget.normalized);
speedModifier = Mathf.Clamp01(speedModifier);
direction = forward * Parameters.MaxSpeed * speedModifier;
m_pPlayer.GetComponent<CharacterController>().SimpleMove(direction);
Here is my updated steering behaviour
//calculate the direction vector to steering tagert
directionToTarget = steeringTarget - m_pPlayer.transform.position;
directionToTarget.y = 0;
directionToTarget = directionToTarget.normalized;
//calculate the direction vector to look position and rotate the player
directionToTrackTarget = trackPos - m_pPlayer.transform.position;
directionToTrackTarget.y = 0;
directionToTrackTarget = directionToTarget.normalized;
//rotate the player
if (directionToTrackTarget != Vector3.zero) {
m_pPlayer.transform.rotation = Quaternion.Slerp (m_pPlayer.transform.rotation, Quaternion.LookRotation (directionToTrackTarget), Parameters.MaxRotationSpeed * Time.deltaTime);
m_pPlayer.transform.eulerAngles = new Vector3 (0, m_pPlayer.transform.eulerAngles.y, 0);
}
Vector3 forward = m_pPlayer.transform.TransformDirection(Vector3.forward);
float speedModifier = Vector3.Dot(forward, directionToTarget.normalized);
speedModifier = Mathf.Clamp01(speedModifier);
directionToTarget = forward * Parameters.MaxSpeed * speedModifier;
m_pPlayer.GetComponent<CharacterController>().SimpleMove(directionToTarget);
I am using state machines and this is the experimental keeper state. In this state the keeper check if he is at his target. If he is at his target he tracks the ball("which works fine") else he has to move back to his position which is not workin.
// Update is called once per frame
override
public void Execute (Keeper keeper)
{
//if the player is not at target make sure he moves towards his spawn point
if (!keeper.AtTarget())
{
keeper.Steering().SetTrackTarget(keeper.Ball().transform.position);
keeper.Steering().Arrive();
}
else
{
keeper.TrackTarget(keeper.Ball().transform.position);
}
//if the ball comes in hot region switch to tend goal
if(keeper.SqrDistance(keeper.Ball().transform.position) <= 20 * 20)
//&& !keeper.Pitch().IsGoalKick())
{
//keeper.GetFSM().ChangeState(ExTendGoal.Instance());
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612054909im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy watches player and shoots but bullets will not project.. 0 Answers
How do I make an AI(cube) that follows and rams the player? 0 Answers
A little doubt about creating a Talking BOT that simulates AI, for iOS system using Unity. 0 Answers
How to get a for loop to run once per coroutine? 1 Answer
A* Pathfinding for Top Down project, how do I make enemyAI to rotate towards waypoints? 0 Answers