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 Ouija · Feb 24, 2014 at 01:50 AM · raycastpickupkeycolider

Change Colider to Raycast instead?

Anyone out there know the solution, apparently its real hard to find lol. Right now I have to be standing on a colider to pick up the object. I would much rather just click on the object using a raycast, seems more natural for my kinda game. Suggestions?

 using UnityEngine;
 using System.Collections;
 
 public class DoneKeyPickup : MonoBehaviour
 {
     public AudioClip keyGrab;                            // Audioclip to play when the key is picked up.
     
     
     private GameObject player;                            // Reference to the player.
     private DonePlayerInventory playerInventory;        // Reference to the player's inventory.
     
     
     void Awake ()
     {
         // Setting up the references.
         player = GameObject.FindGameObjectWithTag(DoneTags.player);
         playerInventory = player.GetComponent<DonePlayerInventory>();
     }
     
     
     void OnTriggerStay (Collider other)
     
 
 {
 
 // If the colliding gameobject is the player...
         if(other.gameObject == player)
 {
             
         if(Input.GetButtonDown("Interact"))        
         
 {
 
 // ... play the clip at the position of the key...
             AudioSource.PlayClipAtPoint(keyGrab, transform.position);
             
             // ... the player has a key ...
             playerInventory.hasKey = true;
             
             // ... and destroy this gameobject.
             Destroy(gameObject);
         }
     }
 }
 }
Comment
Add comment · Show 4
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 highpockets · Feb 24, 2014 at 02:22 AM 0
Share

You could use an:

void On$$anonymous$$ouseDown()

event, and send a ray cast from the mouse position out a set amount of units from the camera and if it hits the collider, then pick it up

avatar image Ouija · Feb 24, 2014 at 02:30 AM 0
Share

Thanks bro, I'm new to scripting, I am unsure how to even write that lol

avatar image highpockets · Feb 24, 2014 at 05:31 PM 0
Share

Did you figure it out? Sorry, I was busy when you got back. If you haven't figured it out yet, I can help you out. Just don't want to write the code if you already found a solution

avatar image Ouija · Feb 28, 2014 at 03:01 AM 0
Share

hey man, sorry i didn't get an email saying you replied, no I haven't figured it out yet sadly, you got some ideas?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Romano · Feb 24, 2014 at 02:19 AM

I wrote a post a while ago explaining a little bit what's tricky about trying to do raycast clicks in 2D, there's also a script there that solves the issue:

http://notquiteblackandwhite.com/post/75474540217/how-to-do-2d-mouse-clicks-in-unity

Hope it helps!

Comment
Add comment · Show 6 · 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 Ouija · Feb 24, 2014 at 02:27 AM 0
Share

It is a FPS so I guess that would be 3d not 2d. Would it work the same you think?

avatar image Romano · Feb 24, 2014 at 02:33 AM 0
Share

I'm an assclown, I totally read the word 2D from another question and thought it was this one, sorry...

This particular script only works in 2D but could be modified to use z depth ins$$anonymous$$d of sorting layers/order for 3D.

avatar image Ouija · Feb 24, 2014 at 02:43 AM 0
Share

haha way to funny. We all have those days. If you can think of any solution to mod the script please let me know

Edit: Went to your website the game looks incredible, I wanna play it badlyyyyyy

avatar image Romano · Feb 24, 2014 at 03:37 AM 0
Share

Hehe, cheers. This guy outlines how to do it: http://answers.unity3d.com/questions/42384/raycasting-with-mouse-click-but-nothing-is-happeni.html

Need to make sure whatever you're trying to click on has a box collider component attached.

avatar image Ouija · Feb 24, 2014 at 05:32 PM 0
Share

That would be great if I were using java haha I'm using C# in the code above

Show more comments
avatar image
0

Answer by JeffreyD · Feb 28, 2014 at 07:00 PM

Hey Guys, Here is how I did a RayCast script placed on the camera. It's for MouseInput not Touch but touch is similar. Note you can check for the "tag" of the collision object or the "name" (As seen in the Hierarchy) either one works. Below uses name.

It's in C# but I think I have one kicking around (Similar but not the same) in JS if you need it. I did try this in 2D and it worked. Not sure if that is a correct application but it worked. I've been meaning to post a Q about that.

Anyway, here is also a link to discussion on Touch in 2D using raycast script.. http://answers.unity3d.com/questions/638075/how-to-destroy-object-by-touch.html

There are several types of Raycasts. The script below uses Physics.Raycast but perhaps Collider.Raycast would be better for your application. I'm not not sure I've not tried them all. Here is a link to the search for Raycast in the Script reference doc. http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=raycast

I have not tested this script below since I took out some stuff to show it to you. The vars could also be defined outside the update function. It all depends on what you're trying to do. Hope it helps.

 using UnityEngine;
 
 using System.Collections;
 
 public class rayCastOnMouseDown : MonoBehaviour 

{

     void Update() 
     {

         // If left mouse is  down.
         if (Input.GetMouseButtonDown(0))
         {
             Debug.Log ("LeftMouse is down");
             
             RaycastHit hitInfo;
             Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
         
             if (Physics.Raycast(rayOrigin, out hitInfo, 20))
             {
                 Debug.Log ("We've hit " + hitInfo.transform.Name);
                 if (hitInfo.transform.name == "MyTargetObject")
                 {    
                     Debug.Log("We hit TARGET 1");
                     // .. do whatever you want to do to Target 1 Object...
                 }
 
                 if (hitInfo.transform.name == "MyOtherTargetObject")
                 {    
                     Debug.Log("We hit TARGET 2");
                     // .. do whatever you want to do to Target 2 Object...
                 }
             }
         }
     }
 }
 

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 Ouija · Mar 01, 2014 at 01:17 AM 0
Share

Thanks for the answer bro! I'm about to play around with it to see if it suits my game needs, basically I want to use the raycast for items that I can pick up in the level. Clicking on them seems nicer then having to be inside a collider, hope this works!! :)

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

21 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

Related Questions

How do I edit springjoint components in script? 1 Answer

Picking Weapons Up with Raycast 1 Answer

Raycast collision on camera see's only part of object 0 Answers

Using Raycast to pickup weapon, but not it's not moving with player 1 Answer

pickup with key 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