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 Joetjah · Jan 25, 2013 at 05:08 PM · javascriptaienemyraycasting

Checking name from hit from Raycast in array

I´m trying to check an array of obstacles on which the enemy AI shouldn´t be walking. Each step, I´m casting a Raycast to the left, front and right. If it hits something, it´s checking the name of the object the Raycast hit in the array. If it´s in that array, the enemy AI shouldn´t be walking there.

This is the code:

 var obstacles = ["Border", "Boundary", "BoundaryFlame"]; 
 var frontAvailable = true;
 var leftAvailable = true;
 var rightAvailable = true;
 var hitFront: RaycastHit;
 if (Physics.Raycast(transform.position, transform.position + transform.forward, hitFront, 1.5)) {
 Debug.Log("I hit this in front: ");
 Debug.Log(hitFront.collider.gameObject.name);
     for (var i = 0; i < obstacles.length; i++)
     {
         if (obstacles[i].IndexOf(hitFront.collider.gameObject.name) > 0)
         {
             Debug.Log("Hit in front!");
             frontAvailable = false;
         }
     }
 }
 var hitLeft: RaycastHit;
 if (Physics.Raycast(transform.position, transform.position +- transform.right, hitLeft, 1.5)) {
 Debug.Log("I hit this on my left: ");
 Debug.Log(hitFront.collider.gameObject.name);
     for (var j = 0; j < obstacles.length; j++)
     {
         if (obstacles[j].IndexOf(hitLeft.collider.gameObject.name) > 0)
         {
             Debug.Log("Hit in left!");
             leftAvailable = false;
         }
     }
 }
 var hitRight: RaycastHit;
 if (Physics.Raycast(transform.position, transform.position + transform.right, hitRight, 1.5)) {
 Debug.Log("I hit this on my right: ");
 Debug.Log(hitFront.collider.gameObject.name);
     for (var k = 0; k < obstacles.length; k++)
     {
         if (obstacles[k].IndexOf(hitRight.collider.gameObject.name) > 0)
         {
             Debug.Log("Hit in right!");
             rightAvailable = false;
         }
     }
 }

The problem is, all 3 booleans stay true. Ingame, I see the AI walking random ways (into walls sometimes). That means the checking of gameobjects isn't working right.

The Debug.Log shows it sometimes hits Boundary(Clone). In that case, it should set the boolean to false. In the array of obstacles, I've placed Boundary, and the index of that name in Boundary(Clone) should be higher then 0? What am I missing?

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

2 Replies

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

Answer by Joetjah · Jan 25, 2013 at 08:35 PM

My if is looking for Boundary(clone) at the beginning of Boundary, rather than the other way around. I was looking for:

 if (hitFront.collider.gameObject.name.IndexOf(obstacles[i]) >= 0)
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
avatar image
0

Answer by Kwaung · Jan 25, 2013 at 06:56 PM

I think delete transform.position from the 2nd argument of the raycast function will help.

like this.

if (Physics.Raycast(transform.position, transform.forward, hitFront, 1.5)) {

Comment
Add comment · Show 1 · 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
avatar image Joetjah · Jan 25, 2013 at 07:58 PM 0
Share

I can't notice a difference by doing so :(

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

10 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Enemy AI With changing Player 0 Answers

Collisions not working when player stands still 1 Answer

How to make basic AI in a 2d game? 4 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