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 Dougieddug · Apr 05, 2013 at 03:55 AM · gameobjectraycastcursorinteraction

Pick up game objects with cursor (raycasting)

I'm a total noob with code, and I was wondering how to use the cursor to interact (pick up/play) with in game objects? Any help would be amazing! Thanks!

Doug

Comment
Add comment · Show 2
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 AlucardJay · Apr 06, 2013 at 05:50 PM 0
Share

Just so I give some constructive advice, I have made videos that demonstrates exactly what you want to know. I suggest using the Spherecast method if your cursor is locked and hidden, the Raycasting method if you are pointing to objects with the mouse :

some basic Raycasting information : http://www.youtube.com/watch?v=Sj4oxfQqzhA

And for destroying objects and incrementing a counter :

  • Raycast : http://www.youtube.com/watch?v=-XIaWwe$$anonymous$$wH4

  • Spherecast : http://www.youtube.com/watch?v=JaOsHCV_SZQ

Loius : mwah-hah-haaa >:D

avatar image Fattie · Apr 09, 2013 at 06:56 AM 0
Share

@doyg can you TIC$$anonymous$$ ANY HELPFUL ANSWER to close out the question please. it's the round tick symbol on the left of answers

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Julian_Spring · Apr 05, 2013 at 06:51 AM

There are lots of different ways to do this :)

So I'll start for you so you can take it from there.

 var hitObject : GameObject;
 var Range : float = 1;
 
 
 
 function Update (){
 
 
 
 var fwd = transform.TransformDirection (Vector3.forward);
 var hit : RaycastHit;
     if(Physics.Raycast (transform.position, fwd, hit, Range)){
         Debug.DrawRay(transform.position, fwd * Range, Color.red);    
             hitObject = hit.collider.gameObject
                     if(Input.GetKeyDown(KeyCode.E)){
 
                         hitObject.transform.parent = gameOject.transfom;
                         return true;
                             
                         
             
             
             }
 
             
         }
 
     }
 
 }

I don't know if this will work not tested but I hope you can take it from there. As I wrote this in notepad :)

Comment
Add comment · Show 4 · 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 Dougieddug · Apr 06, 2013 at 03:43 PM 0
Share

Thanks a bunch mate :) I ran it in the cursor script, but i got an error saying unknown identifier: "gameobject" any tips?

Thanks again, the help is really appreciated!

avatar image Dougieddug Dougieddug · Apr 06, 2013 at 03:43 PM 0
Share

oh wait, sorry, that was me being an idiot, i needed to change gameOject to gameObject XD

avatar image Julian_Spring Dougieddug · Apr 06, 2013 at 04:34 PM 0
Share

Haha, sorry about that. Very glad that I was helpful :P

avatar image fafase · Apr 09, 2013 at 10:04 AM 0
Share

you have a return true in your Update, what is that 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

14 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

Related Questions

Object Interaction 1 Answer

Getting GameObject from a grid (array) using raycasters 0 Answers

How to detect if a raycast ray stop hitting an object 1 Answer

Gameobject follow cursor 2D 0 Answers

Raycasting an empty object 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