Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gcarimando · Jul 16, 2020 at 08:24 PM · aipushedge

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.alt text

desktop-screenshot-1.png (48.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ilagef · Jul 16, 2020 at 09:08 PM

Hey gcarminado,

First we need to get two parameters:

  1. Where is the player.

  2. 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: Fined closet position

  • You need to trigger when the player fall, to inform the AI to stop pushing


best-pos.png (54.8 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by gcarimando · Jul 17, 2020 at 01:44 AM

@ilagef

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ilagef · Jul 17, 2020 at 08:47 AM 0
Share

No problem, Let me know how It goes.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

215 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges