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 Benajben · Nov 14, 2012 at 11:48 PM · raycastrayphysics.raycast

Trying to figure out if one object can see another

I've got two objects in a scene. A spawn point and a player. I only want the spawn point to be active if the player can't see it.

This is a bit of code I'm trying to use.

 Ray ray = new Ray(transform.position, (transform.position - sm.currPlayer.transform.position).normalized);
 RaycastHit hit;
             
 if(Physics.Raycast(ray, out hit, 100.0f))
 {
     if(hit.collider.gameObject.layer == 10) //If we hit a wall
     {
         Enemy z = Instantiate(e, transform.position, e.transform.rotation) as Enemy;
     }
 }

The walls are all on layer 10. So my thinking is that we create a ray from the spawn point to the player. If it collides with one of these walls. That means the player likely can't see the spawn point and go ahead and instantiate an enemy. Otherwise dont. But what is happening is that the enemy is spawning even the player is looking directly at the spawn point. Any help would be much appreciated.

Comment
Add comment · Show 3
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 whydoidoit · Nov 14, 2012 at 11:54 PM 1
Share

Well your ray is pointing backwards - you should be taking transform.position from the target not vice versa.

avatar image Benajben · Nov 14, 2012 at 11:58 PM 0
Share

Alright I changed that and what happened is that now it only spawns if the point is being looked at. But it definitely helps!

avatar image Benajben · Nov 15, 2012 at 12:01 AM 0
Share

Just kidding about the last comment. That worked!

1 Reply

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

Answer by Eric5h5 · Nov 14, 2012 at 11:56 PM

Use Linecast instead of Raycast.

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 Benajben · Nov 15, 2012 at 12:01 AM 0
Share

Oh nice this is exactly what i was looking for!

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

11 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

Related Questions

Raycast help: casting 3 rays, out of the faces of a cube 2 Answers

Problem with raycasting when checking neighbouring objects? 0 Answers

What is the best way to navigate through a grid based level using Raycasting ? 2 Answers

How could I combine melee weapons and rays? 1 Answer

Changing position of a RayCast 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