Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 theabhinv · May 07, 2020 at 06:22 AM · playeraijumpingplayer movementenemy ai

how to make bots stable? ,how to change bots movement?

I want to make bots stable in my game. They jump non stop in-game. Their movement is also not that great like all bots will arrange themselves in one place. I want to control their movement and jump.

I want to be on the ground as well as jump while in defense. what to changes to make in my code so that they do these things. I'm totally new in all os this please help me.

using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq;

public class BotsAI : MonoBehaviour

{

 public LayerMask _groundMask;
 public float range = 0;
 public Transform target;
 public PlayerPhysics player;
 public float minDist = 0.1f;
 public float maxDist = 7.5f;

 public float wantToShoot = 2f;
 public float LHorizontalMovement;

 bool isPlayer = false;

 // Start is called before the first frame update
 public void Start()
 {
     Init();
 }

 public void Init()
 {
     player = GetComponent<PlayerPhysics>();
     wantToShoot = Random.Range(0f, 2f);
     target = FindNewTarget();
 }

 // Update is called once per frame
 void Update()
 {
     if (player.Kira)
     {
         target = player.Kira.transform;
     }

     LHorizontalMovement = 0;

     
     if (target)
     {
         if ((!isPlayer && Mathf.Abs(target.position.x - transform.position.x) > 1f) 
         || (Vector2.Distance(transform.position, target.position) > maxDist)
         || Mathf.Approximately(player.rb.velocity.magnitude, 0)
         )
         {
             target = FindNewTarget();
         }

         MoveTowards();
     }
     else
     {
         target = FindNewTarget();
     }
     wantToShoot -= Time.deltaTime;
 }

 void MoveTowards()
 {

     if (Vector2.Distance(transform.position, target.position) < range && wantToShoot < 0 && isPlayer && wantToShoot < 0)
     {
         Shoot();
     }
     else
     {
         if (Mathf.Abs(transform.position.x - target.position.x) > minDist)
         {
             if (transform.position.x - target.position.x > 0)
             {
                 LHorizontalMovement = -1;
             }
             else
             {
                 LHorizontalMovement = 1;
             }
         }

         if (transform.position.y - target.position.y < -3f)
         {
             player.Jump();
         }
     }
     if (target.position.y - transform.position.y > 1f && !player.OnGround && player.rb.velocity.y <= 0 && wantToShoot > 0)
     {
         player.PressDash(180);
         wantToShoot = Random.Range(1f, 3f);
     }
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (player)
     {
         if (collision.tag == "JumpTrigger")
         {
             player.Jump();

         }
         if (collision.tag == "EdgeTrigger")
         {
             if (player.Manager.SpawnPoints.Count > 0)
             {
                 var n = Random.Range(0, player.Manager.SpawnPoints.Count);
                 target = player.Manager.SpawnPoints[n];
             }
         }
     }
 }

 float TargetDirection(Vector2 t)
 {
     Vector3 dir = ((Vector2)transform.position - t).normalized;
     float angle = Mathf.Atan2(-dir.y, dir.x) * Mathf.Rad2Deg;

     angle = (angle >= 0) ? angle : (360 + angle);


     angle = Mathf.Round(angle / 45f) * 45f;


     return angle - 90f;
 }

 void Shoot()
 {
     player.PressDash(TargetDirection(target.position));
     wantToShoot = Random.Range(1f, 3f);
 }

 public Transform FindNewTarget()
 {
     var whatToFind = Random.Range(0, 4);

     if (whatToFind == 0)
     {
         if (player != null && player.Manager != null && player.Manager.GameController != null && player.Manager.GameController.players != null)
         {
             var players = player.Manager.GameController.players.Where(x => x != null && x.gameObject.layer != gameObject.layer).ToList();
             if (players != null)
             {
                 int self = players.FindIndex(x => x == player.Manager);
                 if (self >= 0)
                 {
                     players.RemoveAt(self);
                 }
                 var p = players.Where(x => x.actor.activeSelf == true).ToList();
                 if (p != null)
                 {
                     isPlayer = true;
                     int index = Random.Range(0, p.Count);
                     return p[index].transform;
                 }
             }
             else if (player.Manager.SpawnPoints.Count > 0)
             {
                 var spawnPoints = player.Manager.SpawnPoints.ToList().Where(x => Mathf.Abs(x.position.x - transform.position.x) > 1f).ToList();
                 var n = Random.Range(0, spawnPoints.Count);
                 isPlayer = false;
                 return spawnPoints[n];
             }
             isPlayer = false;
             return null;
         }
         if (player.Manager.SpawnPoints.Count > 0)
         {
             var spawnPoints = player.Manager.SpawnPoints.ToList().Where(x => Mathf.Abs(x.position.x - transform.position.x) > 1f).ToList();
             var n = Random.Range(0, spawnPoints.Count);
             isPlayer = false;
             return spawnPoints[n];
         }
         isPlayer = false;
         return null;
     }
     else
     {
         if (player != null && player.Manager != null && player.Manager.GameController != null && player.Manager.GameController.players != null)
         {
             if (player.Manager.SpawnPoints.Count > 0)
             {
                 var spawnPoints = player.Manager.SpawnPoints.ToList().Where(x => Mathf.Abs(x.position.x - transform.position.x) > 1f).ToList();
                 var n = Random.Range(0, spawnPoints.Count);
                 isPlayer = false;
                 return spawnPoints[n];
             }
             isPlayer = false;
         }
         return null;

     }
 }

},

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

0 Replies

· Add your reply
  • Sort: 

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

230 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 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

NavMeshAgent does not follow player 0 Answers

Player wont jump (visual script),2d player does not jump (visual script) 1 Answer

How do I jump? 0 Answers

Switch turns between the Player and AI 0 Answers

How to prevent player from moving in air while jumping in place?? 0 Answers


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