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
-1
Question by logang · Jan 31, 2013 at 10:31 PM · 2dtransforminputmousebeginner

I am making a 2d game and I want to make it where to can pick up bricks with your mouse

I am making a 2d game and I want to make it where to can pick up bricks with your mouse and move them. Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Julien-Lynge · Jan 31, 2013 at 10:33 PM

That sounds like a fun game! I'm sure others would like to do the same thing.

By the way, if you don't ask a question, you don't get an answer.

Comment
Add comment · Show 5 · 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 logang · Feb 01, 2013 at 02:11 AM 0
Share

Lol i want to know how to make it like that.

avatar image landon912 · Feb 01, 2013 at 02:41 AM 0
Share

He was mocking your question. Your new here :p

avatar image logang · Feb 01, 2013 at 02:43 AM 0
Share

Yeah. I was just asking if someone knew

avatar image Julien-Lynge · Feb 01, 2013 at 02:43 AM 2
Share

@logang,

Here's a better answer for you. I hope it helps!

Your post (as it appears now) isn't something we can give you a quick answer for, and isn't really appropriate for UnityAnswers. The UnityAnswers philosophy is:

"[Unity Answers] is a place to ask specific questions that have specific answers. The forum is a better place to post discussions and non-technical questions."

It sounds like what you're looking for is Unity training, rather than a specific answer. I would suggest visiting the following training websites to find the one that best helps you move forward. In addition to the sites below, you can always search YouTube, which has a large number of user-created Unity tutorials.

  • 3DBuzz (hover over the Unity dropdown) - http://www.3dbuzz.com/vbforum/sv_home.php

  • Lynda - https://www.lynda.com/

  • BurgZergArcade - http://www.burgzergarcade.com/

  • Unity3DStudent - http://www.unity3dstudent.com/

  • UnityGems - http://unitygems.com/

avatar image landon912 · Feb 01, 2013 at 02:49 AM 0
Share

You forgot UnityCookie!?!? :p

avatar image
1

Answer by landon912 · Feb 01, 2013 at 02:47 AM

     Break it down into smaller parts,
 `   `Learn how to get if the mouse is over the brick..pisss(OnMouseOver) 
     Then learn to get if you click on it...pisssss(Input.GetMouseButtonDown, inside of the above).
     Then learn to get the mouse position...pissss(Input.mousePosition, I think!? I'm not on pc) 
     Then learn to move the brick...pissss(transform.Translate) 

. Next time I won't be so kind. Don't ask us to do your work...wait you didn't even ask a question. :p

I put in in a code format so it is positioned right, it's hard to do on mobile ;)

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 logang · Feb 01, 2013 at 03:09 AM 0
Share

Thanks. and i did not ask you to do my work. I asked if you could help.

avatar image
1

Answer by AlucardJay · Feb 01, 2013 at 05:11 AM

I guess you could call the comments here a lesson on how to ask a detailed question on Unity Answers. I have to interject as I feel OnMouse functions to be unreliable, what you should learn about is Raycast and Collider. Collider becomes a big thing in Unity, so is good to learn and understand. Raycasting returns so much information, it is harder to do things without it.

So now there are some things to start considering : colliders help identify objects and allow them to react physically; raycasts are great for detecting colliders and returning useful information about them.

Unity Scripting Reference links :

  • Colliders : http://docs.unity3d.com/Documentation/ScriptReference/Collider.html

  • Raycast : http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

Those are probably the most confusing links to read apart from Transform and GameObject, but use the Unity Scripting Reference (API) and things get explained well there.

But to help you start, I am going to do what is normally not allowed on this 'site, show you how to put all the different pieces together to do what you want.

Start with a mouse input : http://docs.unity3d.com/Documentation/ScriptReference/Input.html : leads to > > : http://docs.unity3d.com/Documentation/ScriptReference/Input.GetMouseButtonDown.html

 function Update() 
 {
     if ( Input.GetMouseButtonDown(0) )
     {
         Debug.Log( "Pressed left click." );
     }
 }

Now we want to select a brick, and remember that brick. For this we store a reference to it. For now you are probably just moving the brick, so we shall store the type that is Transform.

 var selectedBrick : Transform // a variable that is typecast to Transform to store the selected (chosen) brick
 

now for the fun part, to use raycast to find the brick that is clicked then store a reference to it ! You have the raycast link above. Scroll down to the very last example :

 var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // from the camera in a direction based on mouse screen position
 var hit : RaycastHit; // special variable type that returns information from colliders that were raycast
 if ( Physics.Raycast( ray, hit, 100 ) ) // if ( a ray in the input direction, with the ray hit information to return, for a distane of 100 units from the origin )
 {
     Debug.DrawLine( ray.origin, hit.point ); // show a line from the start to where the ray first hit some collider
     
     Debug.Log( "ray hit (name) : " + hit.collider.gameObject.name ); // the name of the gameObject of the collider that was hit
     Debug.Log( "ray hit (tag) : " + hit.collider.gameObject.tag ); // you can find out lots about the object based on its components
 }

Some interesting things here that come from one small raycast script. The links :

  • Ray : http://docs.unity3d.com/Documentation/ScriptReference/Ray.html

  • RaycastHit : http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html

  • Input.mousePosition : http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html

  • and Debug.DrawLine (excellent way to see what a raycast is doing while testing) : http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawLine.html

So put that raycast after a mouse input and it looks like this :

 var selectedBrick : Transform // a variable that is typecast to Transform to store the selected (chosen) brick
 
 function Update() 
 {
     if ( Input.GetMouseButtonDown(0) )
     {
         Debug.Log( "Pressed left click." );
         
         var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // from the camera in a direction based on mouse screen position
         var hit : RaycastHit; // special variable type that returns information from colliders that were raycast
         
         if ( Physics.Raycast( ray, hit, 100 ) ) // if ( a ray in the input direction, with the ray hit information to return, for a distane of 100 units from the origin )
         {
             Debug.Log( "ray hit (name) : " + hit.collider.transform.name ); // the name of the gameObject of the collider that was hit
             
             // Store a reference to that brick
             selectedBrick = hit.collider.transform;
         }
     }
 }

So really I havn't written your script, Unity has! When you get better at raycasts you can look at Layers and LayerMask , just type them into the search box at the top-left of any Unity Scripting Reference link on this answer. Now this is only half an answer but alot to think about and get started.

If you edit your question to include what you have tried so far, and provide code you are using, then you will have a better chance for better answers.

Definitely watch the unity3Dstudent series. Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/

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

12 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

Related Questions

How to keep my cursor inside window boundaries 0 Answers

mousePosition.x value type. (Equations involving coordinates) 1 Answer

Moving an object from it's current position to the position of the mouse cursor. 0 Answers

Dragging a 2D sprite with touch 1 Answer

Input.GetButton not working 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