• 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
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

People who like this

0 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=-XIaWweMwH4

  • 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 TICK ANY HELPFUL ANSWER to close out the question please. it's the round tick symbol on the left of answers

1 Reply

  • Sort: 
avatar image

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
Loius

People who like this

1 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?

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Two raycast script put together into one 0 Answers

Collision Detection When Picking Up GameObjects 1 Answer

Raycast Collision - Getting Custom Values from GameObject 1 Answer

Using a Empty GameObject "Grouper" - Won't Ignore Raycast 1 Answer

How to assign a game object with 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