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 /
  • Help Room /
avatar image
0
Question by RandomCowch · Aug 13, 2018 at 01:18 PM · raycastainavmeshdestination

AI Raycasting Patrol and Chase,AI enemy raycast help

I have an AI that patrols over the navmesh to certain objects at random around the scene, it goes to one then waits 'x' seconds before picking another and moving on (this works fine). I have been trying to get it to raycast in front then change the destination to the player as soon as it hits something tagged 'Player' (again, can do this on its own), then check the last seen location of the player when it loses sight.


My current workaround for this is to have a cube that matches my players coordinates whenever the player gets hit by raycast, and although this sort of works (it follows the cube until it loses sight of it) then immediately carries on patrolling instead of waiting 'x' seconds.

I'm quite new to this but would appreciate any help.


This is my script though it is very jumbled since I've been trying to find solutions:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class basicAI : MonoBehaviour
 {
     public Transform[] moveSpots;
     private int randomSpot;
     private float waitTime;
     public float startWaitTime;
     public float raydist;
     Ray sightRay;
     RaycastHit rayHit;
     public Transform enemy;
     public Transform lastSighting;
     // Use this for initialization
     void Start()
     {
         waitTime = startWaitTime;
         randomSpot = Random.Range(0, moveSpots.Length);
         lastSighting.position = enemy.transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
 
         this.gameObject.GetComponent<NavMeshAgent>().destination = moveSpots[randomSpot].position;
 
         sightRay = new Ray(transform.position, transform.forward * raydist);
         Debug.DrawRay(transform.position, transform.forward * raydist);
         Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out rayHit);
 
         if (rayHit.transform.CompareTag("Player"))
         {
             lastSighting.transform.position = rayHit.point;
             this.gameObject.GetComponent<NavMeshAgent>().destination = lastSighting.transform.position;
             if (this.gameObject.GetComponent<NavMeshAgent>().remainingDistance < 0.2f)
             {
                 if (waitTime <= 0)
                 {
                     waitTime = startWaitTime;
                 }
                 else
                 {
                     waitTime -= Time.deltaTime;
                 }
             }
 
         }
 
         if (this.gameObject.GetComponent<NavMeshAgent>().remainingDistance < 0.2f)
         {
             if (waitTime <= 0)
             {
                 randomSpot = Random.Range(0, moveSpots.Length);
                 this.gameObject.GetComponent<NavMeshAgent>().destination = moveSpots[randomSpot].transform.position;
                 waitTime = startWaitTime;
             }
             else
             {
                 waitTime -= Time.deltaTime;
             }
         }
     }
 
 
 }

,I am trying to make an enemy AI that patrols (via NavMesh) to certain points around the area, waiting 'x' seconds before moving on to the next location (this bit works fine on its own) then has a raycast that changes the destination of the navmesh agent when hitting objects tagged with 'Player'. Then, when the raycast no longer hits the player, the destination is changed to the last seen position of the player.


My current workaround was to create a box that matched the players position every time the player made contact with the raycast, which works, if the enemy hits the box on raycast it follows it, but no matter the distance away as soon as it loses the raycast it goes back to patrolling.


I am quite new here but would like some help. My code is quite messy atm because I've played around with it so much looking for a solution but it might be of some help:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class basicAI : MonoBehaviour
 {
     public Transform[] moveSpots;
     private int randomSpot;
     private float waitTime;
     public float startWaitTime;
     public float raydist;
     Ray sightRay;
     RaycastHit rayHit;
     public Transform enemy;
     public Transform lastSighting;
     // Use this for initialization
     void Start()
     {
         waitTime = startWaitTime;
         randomSpot = Random.Range(0, moveSpots.Length);
         lastSighting.position = enemy.transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
 
         this.gameObject.GetComponent<NavMeshAgent>().destination = moveSpots[randomSpot].position;
 
         sightRay = new Ray(transform.position, transform.forward * raydist);
         Debug.DrawRay(transform.position, transform.forward * raydist);
         Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out rayHit);
 
         if (rayHit.transform.CompareTag("Player"))
         {
             lastSighting.transform.position = rayHit.point;
             this.gameObject.GetComponent<NavMeshAgent>().destination = lastSighting.transform.position;
             if (this.gameObject.GetComponent<NavMeshAgent>().remainingDistance < 0.2f)
             {
                 if (waitTime <= 0)
                 {
                     waitTime = startWaitTime;
                 }
                 else
                 {
                     waitTime -= Time.deltaTime;
                 }
             }
 
         }
 
         if (this.gameObject.GetComponent<NavMeshAgent>().remainingDistance < 0.2f)
         {
             if (waitTime <= 0)
             {
                 randomSpot = Random.Range(0, moveSpots.Length);
                 this.gameObject.GetComponent<NavMeshAgent>().destination = moveSpots[randomSpot].transform.position;
                 waitTime = startWaitTime;
             }
             else
             {
                 waitTime -= Time.deltaTime;
             }
         }
     }
 
 }
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

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

AI chase/follow problem 0 Answers

How do I affect multiple objects in a single click? 0 Answers

NavmeshDestination not changing. 0 Answers

Problem with NavMesh 0 Answers

How change the destination point if it is inside an obstacle? 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