Hey guys, I want to create a simple Raycasting based AI. But i don't know how to do it I have a simple AI script which tells my enemy where he should go and where he should look at: using UnityEngine; using System.Collections; public class AIFollower : MonoBehaviour { public GameObject target; public float speed; public float rotateSpeed; public float minDistance; public float viewDistance; public float fromCenterToSurface; public float fallSpeed; public float range; public int currentDamage; public Transform attackDot; public float attackTimer; public float coolDown = 2.0f; private bool playerSpotted = false; private Vector3 lastPlayerPos = Vector3.zero; // Use this for initialization void Start () { target = GameObject.FindWithTag("Player"); } // Update is called once per frame void Update () { if(attackTimer > 0) attackTimer -= Time.deltaTime; if(attackTimer < 0) attackTimer = 0; Walk(target.transform.position); RotateTo(target.transform.position); SurfaceDetection(); } void Walk (Vector3 targetPosition) { Vector3 velocity; Vector3 moveDirection = transform.TransformDirection(Vector3.forward); Vector3 delta = targetPosition - transform.position; if (delta.magnitude > minDistance delta.magnitude <= viewDistance) { velocity = moveDirection.normalized * speed * Time.deltaTime; } else { velocity = Vector3.zero; if(attackTimer == 0) { Attack(range, currentDamage, attackDot.position); attackTimer = coolDown; } } rigidbody.velocity = velocity; } void RotateTo (Vector3 targetPosition) { Quaternion destRotation; Vector3 relativePos; //relativePos = targetPosition - transform.position; Vector3 tap; Vector3 trp; tap = targetPosition; trp = transform.position; tap.y = 0; trp.y = 0; relativePos = tap - trp; destRotation = Quaternion.LookRotation(relativePos); transform.rotation = Quaternion.Slerp(transform.rotation, destRotation, rotateSpeed * Time.deltaTime); } void SurfaceDetection () { RaycastHit hit; if (Physics.Raycast(transform.position, -Vector3.up, out hit, fromCenterToSurface)) { } else { rigidbody.velocity = rigidbody.velocity + (-Vector3.up * fallSpeed * Time.deltaTime); } Debug.DrawRay(transform.position, -Vector3.up * fromCenterToSurface, Color.red); } void Attack(float attackRange, int damage, Vector3 attackPoint) { Vector3 forward = transform.TransformDirection(Vector3.forward); RaycastHit hit; if (Physics.Raycast(attackPoint, forward, out hit, attackRange)) { if (hit.transform.gameObject.tag == "Player") { hit.transform.gameObject.GetComponent<PlayerHealth>().AdjustHealth(currentDamage); } } } } Can someone tell me what i must write to give the enemy pathfinding? This will be really nice Thanks BusterBlader
Code (csharp): using UnityEngine; using System.Collections; public class AIFollower : MonoBehaviour { public GameObject target; public float speed; public float rotateSpeed; public float minDistance; public float viewDistance; public float fromCenterToSurface; public float fallSpeed; public float range; public int currentDamage; public Transform attackDot; public float attackTimer; public float coolDown = 2.0f; private bool playerSpotted = false; private Vector3 lastPlayerPos = Vector3.zero; // Use this for initialization void Start () { target = GameObject.FindWithTag("Player"); } // Update is called once per frame void Update () { if(attackTimer > 0) attackTimer -= Time.deltaTime; if(attackTimer < 0) attackTimer = 0; Walk(target.transform.position); RotateTo(target.transform.position); SurfaceDetection(); } void Walk (Vector3 targetPosition) { Vector3 velocity; Vector3 moveDirection = transform.TransformDirection(Vector3.forward); Vector3 delta = targetPosition - transform.position; if (delta.magnitude > minDistance delta.magnitude <= viewDistance) { velocity = moveDirection.normalized * speed * Time.deltaTime; } else { velocity = Vector3.zero; if(attackTimer == 0) { Attack(range, currentDamage, attackDot.position); attackTimer = coolDown; } } rigidbody.velocity = velocity; } void RotateTo (Vector3 targetPosition) { Quaternion destRotation; Vector3 relativePos; //relativePos = targetPosition - transform.position; Vector3 tap; Vector3 trp; tap = targetPosition; trp = transform.position; tap.y = 0; trp.y = 0; relativePos = tap - trp; destRotation = Quaternion.LookRotation(relativePos); transform.rotation = Quaternion.Slerp(transform.rotation, destRotation, rotateSpeed * Time.deltaTime); } void SurfaceDetection () { RaycastHit hit; if (Physics.Raycast(transform.position, -Vector3.up, out hit, fromCenterToSurface)) { } else { rigidbody.velocity = rigidbody.velocity + (-Vector3.up * fallSpeed * Time.deltaTime); } Debug.DrawRay(transform.position, -Vector3.up * fromCenterToSurface, Color.red); } void Attack(float attackRange, int damage, Vector3 attackPoint) { Vector3 forward = transform.TransformDirection(Vector3.forward); RaycastHit hit; if (Physics.Raycast(attackPoint, forward, out hit, attackRange)) { if (hit.transform.gameObject.tag == "Player") { hit.transform.gameObject.GetComponent<PlayerHealth >().AdjustHealth(currentDamage); } } } }
Thanks for posting it with Tags But my problem is still there Please help I want the enemies to avoid obstacles