- Home /
AI Bouncing off Boundaries & Avoiding Other AI
Hi
I've got two different AI's in my top down shooter which I both want to make better.
Floating Hazards - hit and ricochet off of the invisible boundary's
Enemies - Needs to avoid colliding with each other
I have an Invisible boundary set up for the player and the Hazards. Enemies just follow the player but are libel to collide with each other.
Hazards: The hazards currently have Random.range controlling their movement direction which seems to work. If they were hitting a boundary that wasn't invisible I would probably look at an 'OnTriggerEnter (Collider other)' and loop the random.range when the hazard hit the boundary. But with it being invisible I don't quite know what to use instead?
Enemies: I would have liked these to move around following the player and avoiding each other. I see multiple ways of doing this but unsure of the best way. I see a lot of things about having 2 colliders and ray casts which make sense to me but unsure how to implement those.
Neither of these are major and have seen information all over the place but I'm just unsure of the best way and how to implement. I'm not a total noob to programming but I'm no where near fluent.
Any help offered would greatly appreciated. Cheers :)
EDIT for the hazard I have tried putting the boundary positions into a variable and having that in an if statement but that didn't work and I tried a couple of other ways but the way i'm doing it is probable wrong. I can't be far off. This is the code I've been working with. I may be approaching this all wrong, don't be afraid to say I'm totally wrong.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class HazardBoundary
{
public float xMin, xMax, zMin, zMax;
}
public class AsteroidMover : MonoBehaviour
{
public float speed;
public float tumble;
public HazardBoundary boundary;
void Start ()
{
Vector3 randomDirection = new Vector3(Random.Range(-1, 2), 0.0f, Random.Range(-1, 2));
rigidbody.velocity = randomDirection * speed;
}
void Update()
{
//boundary
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
//been trying to recall the code in the start with an if statement
if ()
{
Vector3 randomDirection = new Vector3(Random.Range(-1, 2), 0.0f, Random.Range(-1, 2));
rigidbody.velocity = randomDirection * speed;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Scene Change Collision 2 Answers
Troubleshoot C# collision trigger 0 Answers
Fist Passing Through The Target 2 Answers
Basic Collision, Object + First Person 2 Answers