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 Posly · Feb 16, 2012 at 08:51 PM · raycastraycasthitrayhitcast

How to get information from raycast?

I'm making an FPS, right now I'm working on picking up guns on the floor. I got it to fully function by using the function "OnMouseEnter" and "OnMouseExit", but I just used those for testing purposes, now I want a raycast from the camera and if the raycast hits a gun, it switches the weapon. I was looking at the scripting reference but I couldn't find enough information on RayHit, or something like that. I already know how to cast a raycast, but I don't know how to get information from it, so I need help with that. All I need to know is how to check if the raycast is with a gun. I'm using javascript. Thanks!

Comment
Add comment · Show 1
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 sbroscious · Jul 19, 2014 at 04:17 PM 0
Share

This will be in C#, sorry I know 0 javascript. I will try to make it as easy to follow as possible because I know that I have learned from javascript even though I know none.

First you want to cast a ray from your camara, probably cast to a distance straight forward so you'd need:

 public float rayCastDistance = 100f; //or whatever distance you want
 public RaycastHit yourHitInfo; 
 Ray rayOrigin = Camera.main.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));//sets the rays origin to the middle of your screen


then, now that those are declared:

 if (Physics.Raycast (rayOrigin, out yourHitInfo, rayCastDistance)) 
 { 
 //do something with yourHitInfo like
 if(yourHitInfo.collider.gameObject.tag=="gun")//if your raycast hits a gameObject tagged gun
     {
     //switch weapon
     }
 }











2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Anxo · Feb 16, 2012 at 09:55 PM

http://unity3d.com/support/documentation/ScriptReference/RaycastHit.html

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 mpavlinsky · Feb 16, 2012 at 10:35 PM

You need to use information from your camera when generating the raycast like:

 if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.rotation, 1000)) {
     Debug.Log("Hit.");
 }
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

8 People are following this question.

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

Related Questions

How to get a Vector3 from a RaycastHit variable? 1 Answer

Fall collision force with help of raycast 0 Answers

Exclude tag from raycast 1 Answer

Need Help with RayCast. No Vector? 1 Answer

Trouble with Ray & Raycasting and AddForce() 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