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
4
Question by Tarzerix · Feb 14, 2014 at 12:36 AM · click to move

How to move an 2D object to mouse click position in C#?

Hi,

after quite some time of searching and trying out several examples (without success), I thought I give it a try and ask the community myself… I imagine this to be a newbie problem for many of you, but I guess that's what I still am at the moment…

My Problem:

(Unity 4.3)

I have a 2D underwater scene with a fish.

The fish in its idle behaviour should be randomly swimming around, float, continue swimming and so on…

When I click /tap somewhere under water the fish should now swim to that point in a certain speed.

When I DOUBLE click somewhere the fish should swim to that point just a bit faster.

Can you give me advice on how to simply let the fish move to the clicked position, or do you know a tutorial / example

regarding this issue? Im learning C# right now, so a solution in C# would be important. I already tried several examples including the one from unify but it somehow never worked as desired… Do I need raycasting for this, and if so, how do I set it? Does anybody have an idea? Help me ObiWan you're my only hope!
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
11

Answer by robertbu · Feb 14, 2014 at 01:16 AM

There is no difference between 2D and 3D with respect to moving to the mouse position. An Orthographic camera does simplify the code a bit (2D or 3D). Here is a bit of starter code. If you Google "Unity3d move towards mouse position," you will find other solutions.

 using UnityEngine;
 using System.Collections;
 
 public class GotoMouse : MonoBehaviour {
 
     public float speed = 1.5f;
     private Vector3 target;
 
     void Start () {
         target = transform.position;
     }
     
     void Update () {
         if (Input.GetMouseButtonDown(0)) {
             target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             target.z = transform.position.z;
         }
         transform.position = Vector3.MoveTowards(transform.position, target, speed * 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 Tarzerix · Feb 21, 2014 at 12:30 AM 0
Share

Thank you very much! This definitely gave me a base to build on!

avatar image erayalakese · Aug 29, 2015 at 05:47 PM 0
Share

@robertbu , can you please tell me why we are not using Input.mousePosition directly but using ....ScreenToWorldPoint(Input.mousePosition) in line 15 ? Thank you.

avatar image fafase · Aug 29, 2015 at 05:53 PM 1
Share

mousePosition is in screen coordinate, with 0,0 in the top left. You need to convert that to world position using a matrix. Unity offers that method to perform the conversion.

avatar image FriendlyFPSCreator · Jul 16, 2016 at 12:24 AM -1
Share

The script will not allow the object to go backwards. I have tried tinkering with the script but have not been able to accomplish this see$$anonymous$$gly simple feat. Can you post a modified version of the script that allows the object to move in all directions? And soon please.

avatar image galihkfb · Mar 26, 2019 at 11:37 PM 0
Share

Thank you very much, I just learned about unity, this is very helpful

avatar image
1

Answer by falconer · Oct 22, 2014 at 05:50 AM

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

Move to Touch / Click Position - The Game Contriver

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 DraxThemSklounst · Feb 17, 2021 at 04:22 PM 0
Share

Somewhat of a shame the link takes you to a web-page trying to install add ons to chrome.

Does anyone have a legit link that would explain this?

avatar image
1

Answer by UnityTrouble · Dec 27, 2016 at 10:39 PM

Hi, we made a 2d/2.5d point and click and it is now available on the asset store at https://www.assetstore.unity3d.com/en/#!/content/76020

It might help you with your game. We also take requests of code so let us know what you need and we might be able to add it to the asset store.

holwerda.inc@gmail.com hwaus.com

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

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

Related Questions

How can I create an infinite 3D field of objects? 0 Answers

selecting bones with mouse 0 Answers

Click to Move - Collision Help 0 Answers

NavMeshAgent stop 1 Answer

Max Mouse Clicks Question 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