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 TheFrankman123 · Apr 06, 2012 at 09:34 PM · raycastraycasthit

[RESOLVED]Raycast problem

I have an enemy that goes between wapoints and if the player is in a certain area then chase. However i want to do a raycast from the enemy to the player so that if the player is standing behind a wall but still in the chase distance then it wont chase the player.

It seems very simple but for some reason the raycast is getting the better of me. I want it to change a boolean to false if the enemy can see it but there isn't a wall between.

here's the snippet of my script i am working on. I have defined the player as a transform with tag Player The draw line works.

 function Update () {
     
    var hit : RaycastHit;    
     Debug.DrawLine(transform.position, player.position, Color.red);
     
     if(Physics.Raycast(transform.position, player.transform.position, hit)) {
         
         if(hit.collider.gameObject.name == "player") {
         
             touchingWall = false;
             Debug.Log("touching wall false");
         
         } else {
         
             touchingWall = true; 
             Debug.Log("touching wall true");
         
         }
     }
    }
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 aldonaletto · Apr 07, 2012 at 02:14 AM

@rutter is right: usually Linecast is the best choice in this case - but you must check if the object hit by the line is the target you're looking for: if it's not, you have something in between.

var target2 : Transform;

function Update () { var hit: RaycastHit; if (Physics.Linecast(transform.position, target2.position, hit)){ // if line hits something... if (hit.transform == target2){ // and it's the target... print("I can see you!"); } else { print("Something hides the target"); } } }

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 TheFrankman123 · Apr 07, 2012 at 02:19 PM 0
Share

Thank the lord. Sorry that was such a lengthy process guys. Before i posted this question I had been fiddling with raycasts for about 2 or 3 hours and it all just turned into scribble.

Thanks a lot!

avatar image Kleptomaniac · Apr 07, 2012 at 02:23 PM 1
Share

$$anonymous$$ake sure to accept the answer that most helped you by clicking the tick. :)

avatar image
2

Answer by rutter · Apr 06, 2012 at 09:58 PM

Raycast() doesn't want two positions; it wants one position and a direction (for example, by subtracting your target's position from your own, you can get a vector pointing from yourself to your target).

You might be thinking of `Physics.Linecast()`, which does take two positions.

Comment
Add comment · Show 5 · 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 TheFrankman123 · Apr 06, 2012 at 11:26 PM 0
Share

So what, i would do something along the lines of:

var playerDirection : Vector3 = (player.postion - position.transform);

then

Physics.Raycast (playerDirection, hit, playerDirection.magnitude);

Sorry i have been looking at this all day so really struggling.

avatar image rutter · Apr 06, 2012 at 11:27 PM 0
Share

Either something like that, or just use Linecast(), yes.

avatar image TheFrankman123 · Apr 06, 2012 at 11:32 PM 0
Share

Ok, i will look at the script reference for linecast first. Then try the above. Will amend this question if it's fixed. In the meantime if anyone else has an idea let me know please.

avatar image TheFrankman123 · Apr 07, 2012 at 01:31 AM 0
Share

Ok, I have looked into Linecast and seems like a much more sensible way to do it. I have tried it the way that the script reference suggests and i still get problems. I then learnt that there is issues sometimes with the cast detecting the object it is casting from, so i set they layer on my enemy to ignore casts. But i'm still having problems, they detection seems to be messed up and i can't quite figure why. Despite various debug logs.

I have looked at some answers with the similar problem and i have come up with this:

var target2 : Transform;

     function Update () {
 var hit : RaycastHit;
 Physics.Linecast (transform.position, target2.position, hit);
 if(hit.transform.tag == "Player"){
 print("See");
 
     touchingWall = false;
     
     Debug.Log("not touching anything");
 
 }

}

however for SO$$anonymous$$E reason, despite the fact this has worked for someone else I am getting told that the line "if(hit.transform.tag == "Player") {" is not set to an instance of an object

avatar image Pthorndycraft · Apr 29, 2014 at 07:09 PM 0
Share

You star! Thank you so much, that fixed my problem. For reference:

 if(Physics.Raycast(transform.position, (inco$$anonymous$$gDestination - transform.position) , out hit, $$anonymous$$athf.Infinity))

Works...

 if(Physics.Raycast(transform.position, inco$$anonymous$$gDestination, out hit, $$anonymous$$athf.Infinity))

Doesn't...

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is this way of making a raycast correct 2 Answers

RaycastHit2D.collider is null - why? 1 Answer

How do I change the raycasthit material back? 1 Answer

Does work the raycast on a mesh collider? 3 Answers

Raycasting will not hit some imported meshes with mesh colliders? 2 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