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 /
avatar image
0
Question by Hogge · Jun 22, 2019 at 12:09 PM · raycastarrayai

Raycast cycle problem?

Hello everyone.

I've recently decided to rewrite the AI for my game to make it more advanced, more optimized and reduce the complexity of the code. One thing I've decided to do is to reduce the amount of raycasts being made every frame. So instead of each enemy checking if they can see the player every single frame, I've decided to do it the other way around.

What my code is intended to do is to create an array containing all living enemies in my level, then cast from the player to one enemy per cycle of the code, Once every enemy in the array has been checked, the array is rebuilt in case any enemies have been killed or others have spawned.

This part of the script seems to work just fine. However, I do seem to have come into a spot of bother with the ray tracing. For now, I just want a light to come on above the player's head when there's a clear line of sight between the player and an enemy in the array, basically to check that it works. But it doesn't. For whatever reason, my raycast always turns up negative.

This is my script, it's attached to an empty object placed above the protagonist's head.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyVisionChecker : MonoBehaviour
 {
     public GameObject[] ActiveEnemies;
     int CurrentEnemy = 0;
     public Light Indicator;
     // Start is called before the first frame update
     void Start()
     {
         ActiveEnemies = GameObject.FindGameObjectsWithTag("Enemy");
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         if (CurrentEnemy == ActiveEnemies.Length)
         {
             CurrentEnemy = 0;
             ActiveEnemies = GameObject.FindGameObjectsWithTag("Enemy");
 
         }
         
        
         RaycastHit hit;
 
        
             if (Physics.Raycast(transform.position, (ActiveEnemies[CurrentEnemy].transform.position - transform.position), out hit, 100))
             {
                 if (hit.transform == ActiveEnemies[CurrentEnemy])
                 {
                 Indicator.intensity = 2.0f;  
                 }
             else
             {
                 Indicator.intensity = 0.0f;
             }
             
         }
         else
         {
           
         }
 
         CurrentEnemy++;
         Debug.Log("Current enemy: " + CurrentEnemy.ToString() + "Number of enemies: " + ActiveEnemies.Length.ToString());
     }
 }
 
Comment
Add comment · Show 4
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 Captain_Pineapple · Jun 23, 2019 at 01:49 PM 0
Share

I see you do not use a Layermask on your raycast. Did you put in a debug.Log to check what you are hitting? $$anonymous$$ight be the raycast get stuck on your player character. By your current code you will never know.

avatar image Hogge Captain_Pineapple · Jun 24, 2019 at 02:36 PM 0
Share

O$$anonymous$$, so I've added Debug.DrawLine(transform.position, (ActiveEnemies[CurrentEnemy].transform.position - transform.position), Color.green);

According to that, my rays aren't even remotely pointing towards the enemies. I've been fiddling with it for quite a while, but I just can't wrap my head around it :/

avatar image Captain_Pineapple Hogge · Jun 25, 2019 at 09:00 PM 0
Share

You are aware that drawline will draw a line from a startpoint to an endpoint. You are giving a startpoint and a direction. You might want to change that call to DrawRay ins$$anonymous$$d

Show more comments

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

221 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

Related Questions

Visible GameObjects for AI 0 Answers

Array index is out of range and Raycast question 0 Answers

Waypoint problems 1 Answer

Need Help making Pong AI beatable 1 Answer

My AI drops stuffs on death even when not supposed to 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