Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by BMRX · May 14, 2015 at 03:23 PM · raycasthitfsm

Object move on RaycastHit

So I'm trying to make my Enemy AI flee if a Raycast from the player hits the Enemy.

This is all within a working State Machine so lets get past that.

I use a NavMesh for the enemy movement if you're wondering.

 if (player.GetComponent<Interact>().LookAt())
         {
             Debug.Log("Player See's Me!");
         }

This is what is inside my "flee state".

 public bool LookAt()
     {
         Debug.DrawRay(transform.position, transform.forward * 100, Color.yellow);  //Debug raycast   
         if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) //Raycast)
         {
             hit.transform.CompareTag("Enemy");
             return true;
         }
 
         return false;
     }

This is inside a script that is attached to my Player Camera.

LookAt() is called within Update().

I have never tried anything like this so this is beyond my basic understanding.

This gives me a NullReferenceException within Unity. So it obviously does not work.

Does anyone have a better suggestion for this? I'm not too worried about making the NavMeshAgent move I just want this DebugLog to appear. :P

Comment
Add comment · Show 1
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
avatar image BMRX · May 14, 2015 at 05:37 PM 0
Share

I'm now of the belief that I have approached this all wrong. I feel that I should have my enemy use a Spherecast ins$$anonymous$$d. I will return if this results in nothing.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BMRX · May 14, 2015 at 06:22 PM

ShpereCast was not the answer,

 public override void Act(Transform player, Transform npc)
     {
         RaycastHit hit;
         Collider[] colli = Physics.OverlapSphere(npc.transform.position, 50f);
         foreach (Collider col in colli)
         {
             if (Physics.Raycast(npc.transform.position, (col.transform.position - npc.transform.position), out hit, 50f))
             {
                 if (hit.transform.CompareTag("Player"))//player detector
                 {
                     Debug.Log("Player Spotted");
                 }
             }
         }
     }

This was.

If someone posts a better answer than this I'm more than willing to give the green to them. :D

EDIT/ Didn't think about it but, I need this only to work if the AI has no colliders in between it and the player. Essentially I want it to look for cover from the Players fire. This could however work fine for some sort of panic state. Just have the agent in your navmesh run to a random direction. If you use:

 Vector3 position = new Vector3(Random.Range(-40.0F, 264.0F), Random.Range(6.0F, 10.0F), Random.Range(-200.0F, 95.0F));
 agent.SetDestination(position);

The agent/AI stutters around the map like its dancing or something. I'm certain if you used a better method to set the destination it would move more smoothly.

Comment
Add comment · Share
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

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

19 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

Related Questions

Raycashit Variable make empty/null/clear? 1 Answer

RayCast to all directions 4 Answers

Is this way of making a raycast correct 2 Answers

Need some help with to grap .x with a Raycasthit (c#) 1 Answer

ray cast returning the wrong object, going through multiple objects 1 Answer


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