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 rct3fan24 · Feb 07, 2014 at 05:11 AM · moveclick

How to make an object move towards the mouse pointer on click? [2D]

Total noob question:

So, the control system I'm looking at is this: The camera follows directly above the player's character, and if the player clicks anywhere, the player's character will move towards that position for the duration of the click. I'm very new to the 2D Unity, can anyone help me with this?

Remember that this is 2D, so "up" is the Z axis.

Comment
Add comment · Show 3
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 Romano · Feb 07, 2014 at 05:32 AM 0
Share

I'm trying to solve the same problem myself. I've got at least part of the way. Are you using sorting layers and sorting orders? They cause a problem with clicks that I should be able to help with. Also was wondering why you have a Z axis if your game is totally 2D? I'm kinda new to Unity as well, so that might be a daft question.

avatar image rct3fan24 · Feb 07, 2014 at 03:50 PM 0
Share

@Romano About the Z axis thing, I was just re$$anonymous$$ding people that X and Y were the planes I need to move on, and not Z.

I DO have a parallaxing background that requires background layers on different Z axi, and a Perspective camera.

avatar image falconer · Oct 22, 2014 at 05:50 AM 0
Share

This is pretty simple. You can use the Vector3.Lerp function to achieve this. Use raycasting to get the mouse click position or the touch position. Then use the initial and the final position in the lerp function. The initial position being the position that the gameobject is at now and the final position being the click / touch position. You can find the article by The Game Contriver on the same here

$$anonymous$$ove to Touch / Click Position - The Game Contriver

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by robertbu · Feb 07, 2014 at 07:06 AM

You are not asking for rotation, so there is no difference between 2D and 3D. This bit of sample code assumes an Orthographic camera.

 #pragma strict
 
 public var moveSpeed = 2.0;  // Units per second
 
 function Update () {
     if (Input.GetMouseButton(0)) {
         var targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         targetPos.z = transform.position.z;
         transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
     }
 }
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 rct3fan24 · Feb 07, 2014 at 03:45 PM 1
Share

I'm actually using a Perspective camera, because I have a parallaxing background, on different Z axi, which didn't work with an Orthographic camera.

Will this still work?

And yeah, I don't need the object to rotate. You play as a black hole, so you're basically a circle.

avatar image robertbu · Feb 07, 2014 at 03:54 PM 1
Share

Here is a bit of code for a perspective camera. It assumes the camera has a rotation of (0,0,0):

 #pragma strict
  
 public var moveSpeed = 2.0;  // Units per second
  
 function Update () {
     if (Input.Get$$anonymous$$ouseButton(0)) {
         var pos = Input.mousePosition;
         pos.z = transform.position.z - Camera.main.transform.position.z;
         pos = Camera.main.ScreenToWorldPoint(pos);
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, pos, moveSpeed * Time.deltaTime);
     }
 }
avatar image sean244 robertbu · Jun 05, 2018 at 05:17 PM 0
Share

This solved a similar problem I was having. Thank you :)

avatar image JellyBellyFred · Jul 08, 2014 at 04:25 PM 0
Share

I was using @robertbu 's orthographic camera code, but my object becomes invisible after a few seconds, though other things can still interact with it.

avatar image mudassar10147 · Mar 20, 2016 at 08:50 AM 0
Share

Thankyou so much... love you friend

avatar image
0

Answer by sagivo · May 20, 2014 at 03:27 PM

have a look at Vector2.MoveTowards

 transform.position = Vector2.MoveTowards(transform.position, Target.transform.position, Speed*Time.deltaTime);

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

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

Related Questions

move a button On click - rather than on game start 1 Answer

Click to move on a sphere (planetary gravity) 0 Answers

How Can I write a Click To Move Script? 2 Answers

How do I move objects in one executed turn? 1 Answer

I get errorsfor my Click to Move Script 0 Answers


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