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 a_flux · Oct 05, 2013 at 06:19 PM · movementmouseclickeventsonclick

using mouse to move an object

I have created an object in my scene, what exactly I wanna do is that, I want to click on my object using my left click (mouse) and the object moves towards a certain point, then clicking back again on the same object to return to it's position. which method can I use to do this function and if possible can somebody include an example of a script or something, I am totally new to this unity game engine.

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 g0tNoodles · Oct 05, 2013 at 09:39 PM 0
Share

Can you be a little more specific about what you are wanting? Are you wanting to click a point in 3D space and move the object or are you wanting to click the object itself and then move it?

avatar image antiquote · Oct 05, 2013 at 09:54 PM 0
Share

Are you saying click and drag the object and have it move? If so, I just figured out how to do this and I'll be glad to help you out.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GambinoInd · Oct 05, 2013 at 10:41 PM

Add a collider to the object you wish to move. You should also put it on a layer that holds all movable objects.

Next you want to create a script that sends a raycast from the camera using something like this?

 int layerMask = (1 << LAYER_FOR_MOVABLE_OBJECTS); 
 RaycastHit hit = new RaycastHit ();
 if(Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 1000, layerMask)) {
 //Get hit.point.x, and hit.point.y, and hit.point.z to grab the position of where it hit
 }

If you are making the game 2D or from a side view, you could dont have to raycast and you could just get the Input.MousePosition and move the object on 2 axis (like x and y) based on the mouse movement. Moving it around in the 3d world requires the code i just posted above, but you're going to have to figure the rest out.

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

Answer by Cherno · Oct 05, 2013 at 11:28 PM

Your box needs a Box Collider and the following script:

 var ObjectLayerMask : LayerMask;//Select the layer your box or whatever is in
 
 var Position1 : Vector3;//leave empty, it gets filled automatically in Start()
 var Position2 : Vector3;//enter the coordinates of your desired location to move towards
 
 var MoveSpeed : float = 4;
 
 var CurrentPos : int = 1;
 
 function Update()
 {
     var ray = Camera.main.camera.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     
     if(Input.GetButtonDown("Fire1"))
     {
         if (Physics.Raycast (ray, hit, Mathf.Infinity, ObjectLayerMask)) 
         {
             Move();
         }
     }
 }
 
 
 
 function Move()
 {
     var StartPosition : Vector3 = transform.position;
     var EndPosition : Vector3;
     
     if(CurrentPos == 1)
     {
         EndPosition = Position2;
         CurrentPos = 2;
     }
     else
     {
         if(CurrentPos == 2)
         {
             EndPosition = Position1;
             CurrentPos = 1;
         }
     }
         
     
     
     var t : float = 0.0;
       
     while (t < 1.0) 
     {
         t += Time.deltaTime * MoveSpeed;
         transform.position = Vector3.Lerp(StartPosition, EndPosition, t);
         yield;
     }
 }
         
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

19 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

Related Questions

Moving an object to the location of a mouse click... 2 Answers

Button doesn't work when returning to scene (android) 1 Answer

Want sprite to move to mouse click position instead of teleporting or moving a few pixels 1 Answer

Extending Unity's UI components 1 Answer

I want OnClick to fire the same frame that the button is clicked 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