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 NutellaDaddy · Mar 22, 2014 at 06:03 PM · c#raycastopencrate

How to use Raycast to open a crate?

How could I make it so that I have a raycast that , when you hover over a gameobject with the tag "Small Crate" and then you press "e" you set this var I have, crateOpen, to true? Sorry I am not very good with Raycasting. I also need it to be so that you can only open it when you are about 2 units away. C Sharp please. And thank you for spending your time to help me out!

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 SkaredCreations · Mar 22, 2014 at 06:24 PM

Here is:

 void Update () {
     if (Input.GetKeyUp(KeyCode.E)) {
         RaycastHit hit;
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
             if (hit.collider.CompareTag("Small Crate") && Vector3.Distance(hit.collider.transform.position, transform.position) <= 2.0f) {
                 crateOpen = true;
             }
         }
     }
 }
 
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 NutellaDaddy · Mar 22, 2014 at 07:45 PM 0
Share

Would a script like this need to be attached to the player?

avatar image SkaredCreations · Mar 22, 2014 at 09:43 PM 0
Share

Yes because it uses transform.position in the calculation of distance, if you want to attach to something else then you need to change it to use the player transform position there.

avatar image
0

Answer by Ozzyel-PT · Mar 22, 2014 at 06:41 PM

If you mean hover the mouse cursor wold be like this:

 function Update () {
     if (Input.GetKeyDown("e")) {
         var hit : RaycastHit;
         // Construct a ray from the current mouse coordinates
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (ray, hit)) {
             if(hit.collider.tag == "Small Crate" && hit.distance > 2)
                 crateOpen = true;
         }
     }
 }

I put first press key for performance. I'm assuming your script is attached to the camera so you have to send the "crateOpen" variable.

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 Ozzyel-PT · Mar 24, 2014 at 06:29 PM 0
Share

Sorry, didn't see you wanted it in C#

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

22 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

need help converting gun script from c# to javascript 1 Answer

Unity asking me /not/ to add out to a raycast 1 Answer

Open and close a drawer with raycast 2 Answers

Layer Mask Not working to open the door 1 Answer

How to send a raycast to an object to give me ammo? 0 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