Enemy AI Issues!!?!
My AI is being stubborn and I was wondering if their is a quick fix to my problem... I have a box colliders on everything from walls to player to AI, but for some reason my Enemy wants to faze through walls, and keeps pushing my character when it's shouldn't... i was wondering if their is a quick fix to this problem I provided the script and a picture of the problem... "NO Error in script"
want my enemy to stop at a distance of .25 away from player... I want my enemy to avoid and not go through walls...
using UnityEngine; using System.Collections;
[RequireComponent(typeof(SphereCollider))] public class EnemyAI : MonoBehaviour {
public float preceptionRadius = 10; public Transform target; public float movementSpeed = 5; public float rotationSpeed = 420; private Transform myTransform; // Use this for initialization void Start () { SphereCollider sc = GetComponent<SphereCollider>(); CharacterController cc = GetComponent<CharacterController>(); if (sc == null) Debug.LogError("No Collider on enemy"); else { sc.isTrigger = true; } if(cc == null) { Debug.LogError("There's no Character Controller"); } else { sc.center = cc.center; sc.radius = preceptionRadius; } myTransform = transform; } // Update is called once per frame void Update() { if (target) { Vector3 dir = (target.position - myTransform.position).normalized; float direction = Vector3.Dot(dir, transform.forward); float dist = Vector3.Distance(target.position, myTransform.position); //Find or Look at Target myTransform.rotation = Quaternion.Lerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime); //Movement myTransform.position += myTransform.forward * movementSpeed * Time.deltaTime; } } public void OnTriggerEnter(Collider other) { Debug.Log("Entered"); if (other.CompareTag("Player")) { target = other.transform; } } public void OnTriggerExit(Collider other) { Debug.Log("Exit"); if(other.CompareTag("Player")) { target = null; } } }
Answer by lloladin · Sep 26, 2015 at 05:41 PM
to make the enemy stop moving when he hits the player i would use line cast and then make a bool moveTorwardsPlayer and set the bool to true if the player is not in the line cast and the same for the wall just if walls are not in the line cast
some Documentation [http://docs.unity3d.com/ScriptReference/Physics.Linecast.html][1]
Example
public bool moveTorwardsPlayer;
public LayerMask MoveBlockers;
public Void FixedUpdate()
{
moveTorwardsPlayer = Physic2D.linecast("FirstPoint.poistion","SecoundPoint", MoveBlockers );
}
public Void Update()
{
if (moveTorwardsPlayer)
{
// Do ai stuff
}
}
this is untested code but it should give you the idea of what to do hope this helps :)
Wait so what you are saying is that if i add this to my script i could apply an if statement into another if statement? ALso can I change the Phisics2D to 3d since its in a 3d field and not 2d,its a 3d game...
yes you can change it to 3d just write Physic.Linecast and fill in the parrmeters and you simple Wrap the if (moveTorwardsPlayer) Around the if (Target) statement
I did what you said as seen here... is it because I added a copy of target.position, myTransform.position?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SphereCollider))]
public class EnemyAI : $$anonymous$$onoBehaviour {
public float preceptionRadius = 10;
public Transform target;
public float movementSpeed = 5;
public float rotationSpeed = 420;
public bool moveTorwardsPlayer;
public Layer$$anonymous$$ask $$anonymous$$oveBlockers;
private Transform myTransform;
// Use this for initialization
void Start ()
{
SphereCollider sc = GetComponent<SphereCollider>();
CharacterController cc = GetComponent<CharacterController>();
if (sc == null)
Debug.LogError("No Collider on enemy");
else
{
sc.isTrigger = true;
}
if(cc == null)
{
Debug.LogError("There's no Character Controller");
}
else
{
sc.center = cc.center;
sc.radius = preceptionRadius;
}
myTransform = transform;
}
public void FixedUpdate()
{
moveTorwardsPlayer = Physics.Linecast(target.position, myTransform.position, $$anonymous$$oveBlockers);
}
// Update is called once per frame
void Update()
{
if (moveTorwardsPlayer)
{
if (target)
{
Vector3 dir = (target.position - myTransform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
float dist = Vector3.Distance(target.position, myTransform.position);
//Find or Look at Target
myTransform.rotation = Quaternion.Lerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//$$anonymous$$ovement
myTransform.position += myTransform.forward * movementSpeed * Time.deltaTime;
}
}
}
public void OnTriggerEnter(Collider other)
{
Debug.Log("Entered");
if (other.CompareTag("Player"))
{
target = other.transform;
}
}
public void OnTriggerExit(Collider other)
{
Debug.Log("Exit");
if(other.CompareTag("Player"))
{
target = null;
}
}
}
hm i seem to have made a mistake the bool moveTorwardsPlayer should be dont$$anonymous$$oveTorwardsPlayer or some better name and the condition should be !dont$$anonymous$$oveTorwardsPlayer but also thats not quite how linecasting works you Draw a line between 2 Objects and then if anything touches that the bool is true else its false what you did is $$anonymous$$oveTorwards player aslong as player is inbetween Target and $$anonymous$$yTransForm so you should think about it like this
dont$$anonymous$$oveTorwardsPlayer = Physics.Linecast(Transform A, transform B, $$anonymous$$oveBlockers);
so 1 way to use it would be to add 2 Child Objects to youre Enemy TransForm a and Transform B and put them infront of the enemy so that when he turns torwards the player they will follow Documentation if you wana read about it :D http://docs.unity3d.com/ScriptReference/Physics.Linecast.html hope it helps :)