- Home /
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