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 Projekt-Krieg · Jul 14, 2013 at 10:14 AM · raycastcollideraiquaternion

My Raycast dont hit.

Hi there.

I try to make a simple Basic AI, that approaches a Target, and turn around if its near enougth, but all it do is approach the Target, until it is in it, and start rotate.

Thats what i got until now.

 public float spacetotarget;
 private GameObject target;

 //approach Target
         
         if(Physics.Raycast(transform.position,target.transform.position,spacetotarget,10)){
             Debug.Log("zunah");
             targetrotation = Quaternion.Inverse(Quaternion.LookRotation(target.transform.position - transform.position));
         }
             
         else{
             targetrotation = Quaternion.LookRotation(target.transform.position - transform.position);    
         }

The Debug.Log dont show up. The Target has a Collider and the Layer 10.

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

3 Replies

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

Answer by Projekt-Krieg · Jul 14, 2013 at 11:19 AM

Find a Way:

the declaration of the Layermask is a bit tricky.

         int targetlayer = 10;
         targetlayer = ~targetlayer;
         Vector3 direction = target.transform.position-transform.position;
         if(Physics.Raycast(transform.position,direction,spacetotarget,targetlayer)){
             targetrotation = Quaternion.Inverse(Quaternion.LookRotation(target.transform.position - transform.position));
         }
             
         else{
             targetrotation = Quaternion.LookRotation(target.transform.position - transform.position);    
         }
Comment
Add comment · Show 2 · 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 Bunny83 · Jul 14, 2013 at 02:00 PM 0
Share

Your layermask makes no sense. You just inverted the mask. So ins$$anonymous$$d of layer 1 and 3 you include all except 1 and 3.

Your value of 10 equals this bitmask:

 00000000 00000000 00000000 00001010

The inverted mask would look like this:

 11111111 11111111 11111111 11110101

If you want to only cast agains layer 10 the mask should look like this:

 00000000 00000000 00000100 00000000
                        |
                        \___________Layer 10 (the 11th layer)

This equals the value of 1024 or 2^10.

I guess you just don't know what you actually want...

avatar image Projekt-Krieg · Jul 14, 2013 at 05:14 PM 0
Share
 private int targetlayer = 1<<10;
 
 
 int bittargetlayer = ~targetlayer;
 if(Physics.Linecast(transform.position,target.transform.position,bittargetlayer));

this is my final solution.

avatar image
0

Answer by Bunny83 · Jul 14, 2013 at 10:35 AM

You have several mistakes here:

  • Raycast uses a position from where to start and a direction vector. However you used a position as direction.

  • The layermask parameter is a bitmask and not a layer index. You effectively are casting against layer 1 (value 2) and layer 3 (value 8).

To fix this you could use a Linecast instead of a Raycast which takes two positions instead of a position and a direction.

When using a Raycast you should calculate the direction vector to your target by calculating

     target.transform.position - transform.position

To get a layermask that only has layer 10 in it (start counting by 0) just use

     2^10

or precalculate the value which equals:

     1024


Here's a woring example:

     Vector3 dir = target.transform.position - transform.position;
     int layermask = 2^10;
     if(Physics.Raycast(transform.position, dir, spacetotarget, layermask))


Comment
Add comment · Show 2 · 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 Bunny83 · Jul 14, 2013 at 10:36 AM 0
Share

$$anonymous$$ake sure your "spacetotarget" is large enough to actually reach the target.

avatar image Projekt-Krieg · Jul 14, 2013 at 10:51 AM 0
Share

Changed my code to:

 Vector3 direction = target.transform.position-transform.position;
         Layer$$anonymous$$ask justhit = 2^10;
         if(Physics.Raycast(transform.position,direction,spacetotarget,justhit)){
             Debug.Log("zunah");
             targetrotation = Quaternion.Inverse(Quaternion.LookRotation(target.transform.position - transform.position));
         }
             
         else{
             targetrotation = Quaternion.LookRotation(target.transform.position - transform.position);    
         }

but it still approaches and rotate in the target.

spacetotarget is 500 so it should work.

avatar image
0

Answer by Griffo · Jul 14, 2013 at 05:26 PM

This is the way I go with layers ..

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

17 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

Related Questions

Why Do Raycasts Ignore NavMesh Agents 4 Answers

Collider Vision AI question. Solved! 0 Answers

Character Controller can pass through Collider 1 Answer

Using a raycast without colliders 0 Answers

About a enemy AI in my game 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