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 HylosUnity · Jan 21, 2018 at 05:48 PM · c#ai

Problems with Enemy AI with Raycasts

Hey!

I am developing a little shooter. To script the KI I've taken a Pyramid for FOV and I'm using the "Is Trigger"-Tool (or whatever it is) to trigger the Enemy, if the Player collides with the Pyramid. When the Player collides with the Pyramid: first a Ray should be casted from the Enemy to the Player to detect if there's something between Player and Enemy. Then, if there's nothing between them, the Enemy should chase the Player and attack him. Otherwise the Enemy shouldn't do anything. So the problem is, that, when the Player is in the Pyramid, the Enemy isn't doing anything. I think he is not casting the Ray. Only, if the Player is in front of him he follows him. Hope you can help me. Here's my Script... (Sorry for spelling mistakes) Btw It worked without the Raycast.

              using System.Collections;
              using System.Collections.Generic;
              using UnityEngine;
              using UnityStandardAssets;
              using UnityEngine.AI;
              
              public class EnemyAI : MonoBehaviour {
              
                  float distance;
                  public Transform target;
                  NavMeshAgent navComponent;
                  public GameObject Enemy;
              
                  void OnTriggerStay(Collider colInfo)
                  {
                      target = GameObject.FindGameObjectWithTag("Player").transform;
                      Ray ray = new Ray (Enemy.transform.position, target.position);
                          
                      if (colInfo.tag == "Player") 
                      {
                          RaycastHit hit;
              
                          if(Physics.Raycast (ray, out hit)) 
                          {
                              if (hit.collider.tag == "Player") 
                              {
                                  navComponent = Enemy.GetComponent<NavMeshAgent> ();
                                  navComponent.SetDestination (target.position);
                              }
                          }
                      }
                  }
                 
                  /*void lookAt()
                  {
                      Vector3 direction = (target.position - Enemy.transform.position).normalized;
                      Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
                      Enemy.transform.rotation = Quaternion.Slerp(Enemy.transform.rotation, lookRotation, Time.deltaTime * 5f);
                  }*/
              }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HylosUnity · Mar 29, 2018 at 09:25 PM

In line one I meant AI :) ... For those guys who wanna know how I made it: There's is a function from Unity called "Linecast": https://docs.unity3d.com/ScriptReference/Physics.Linecast.html

So instead Raycast I used a Linecast.

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

174 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

Related Questions

How do I make AI objects not walk on top of each other? 5 Answers

how to check if an object is betwen enemy and player 1 Answer

HELP!! How to make my Mixamo Character sit on a CHAIR?? A* Pathfinding + Mixamo Animation 0 Answers

Can this script be better optimized? 1 Answer

I need help with AI,Force not working 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