- Home /
How to have AI push player of edge?
Hi!
In my game I want to have an AI enemy try to push the player of the edge (See Image for layout). I was wondering how I would do this. I am new to scripting so help would be greatly appreciated.
Answer by ilagef · Jul 16, 2020 at 09:08 PM
Hey gcarminado,
First we need to get two parameters:
Where is the player.
Where is the closet edge.
For the first one there are two ways:
Solution 1
[SerializeField] float movmentSpeed;
[SerializeField] Transform player;
[SerializeField] Transform floor;
public bool hasPlayerFalled;
private void Awake()
{
hasPlayerFalled = false;
}
void Update()
{
if (!hasPlayerFalled) // push only if the player fall
{
Vector3.MoveTowards(transform.position, player.position, movmentSpeed);
}
}
Solution 2
// AI code
[SerializeField] float movmentSpeed;
[SerializeField] Transform player;
public bool hasPlayerFalled;
private void Awake()
{
hasPlayerFalled = false;
}
void Update()
{
if (!hasPlayerFalled) // push only if the player fall
{
Vector3 deltaPos = player.position - transform.position; // we want to know what are the differences between the enemy and the player position.
Vector3 newPos = transform.position; // this is the new desired position of the enemey, we want the enemy to move toward the player
if (Mathf.Abs(deltaPos.x) > movmentSpeed)
{ // if we are not close enough to the player on the X axis, move toward it.
newPos.x += Mathf.Sign(deltaPos.x) * movmentSpeed * Time.deltaTime;
}
else
{ // if we are close enough we want to slow and only push
newPos.x += deltaPos.x / 2f;
}
if (Mathf.Abs(deltaPos.z) > 0.1f)
{ // if we are not close enough to the player on the Z axis, move toward it.
newPos.z += Mathf.Sign(deltaPos.z) * movmentSpeed * Time.deltaTime;
}
else
{ // if we are close enough we want to slow and only push
newPos.x += deltaPos.x / 2f;
}
}
}
This part only moves the AI toward the player which would make it push it toward one of the edge,
I will update the post later with additional part that explain how to find the closed edge.
In addition you have to set the SerializeFields from the unity panel. After you add this scrip, drag the player GameObject to the new field that the script has. And set the relevant speed of the AI.
Do not forget to add to both of them (AI and Player): 1.3D Collider 2. 3D Rigitbody
Edit: To find the best position to start pushing the player from, you can use this function:
public Vector3 GetPushStardPosition()
{
Vector3 playerFloorDeltaPos = floor.position - player.position; // we want to know the relative location of the player to the floor.
Vector3 playerSize = player.gameObject.GetComponent<Renderer>().bounds.size;
Vector3 aiSize = gameObject.GetComponent<Renderer>().bounds.size;
// We canculate the relative new desired position.
// This is the position that between the player location to the floor center
// On that direction, we want the closet position that have enough "space" for the player and the AI
Vector3 desiredPos = player.position + playerFloorDeltaPos.normalized * (playerSize.x / 2.2f + aiSize.x / 2.2f);
return desiredPos;
}
this will find the position that marked in the image:
You need to trigger when the player fall, to inform the AI to stop pushing
Answer by gcarimando · Jul 17, 2020 at 01:44 AM
Thank You so much. I really appreciate it. It's really heartwarming to know that there are people like you out there who put so much time into helping others.
I will try the scripts out first thing tomorrow and see how it goes and let you know.
Thanks Again!
Your answer
![](https://koobas.hobune.stream/wayback/20220613003338im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I stop a navmesh agent to move my player? 1 Answer
AI vs AI Combat/Detection Method 0 Answers
What is the best AI asset for $50 or less? 0 Answers
Finding a the closest distance between a Ai and multipel enemys 2 Answers
Simple AI Question 1 Answer