• 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 Jajyesin · May 01, 2016 at 09:53 AM · c#2dmovementplayer movementmouseclick

How to make an object move to another object's location in a 2d game?

Hello, I am trying to figure out how to make a game object (like a player) move to another object (like a house or dungeon) that the player clicked on, but only if they clicked on it. Meaning the player can't move around freely. It doesn't really matter how it gets there, so path finding or anything like that doesn't matter.

I'm sorry if this is a stupid thing to ask, or too much to ask of.

Comment

People who like this

0 Show 1
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 Ali-hatem · May 01, 2016 at 10:09 AM 0
Share

similar

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by g__l · May 01, 2016 at 10:52 AM

(EDIT - Sorry this only answers the object moving towards another part, i should have read the full description and not been lazy reading the title)

Well this should be relatively simple with Vector2.MoveTowards() then make your code look something like this

 using UnityEngine;
 using System.Collections;
 
 public class MoveTowards : MonoBehaviour {
     GameObject HouseOrDungeon;
     GameObject Player;
     float speed;
 
     // Update is called once per frame
     void Update () {
         float step = speed * Time.deltaTime;
         Player.transform.position = Vector2.MoveTowards(Player.transform.position, HouseOrDungeon.transform.position, step);
     }
 }

Check out the documentation (here)

Hope that helps but please remeber (do this before asking)

Comment
jsm209
elijahmorgan311

People who like this

2 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 Ali-hatem · May 01, 2016 at 10:57 AM 0
Share

this is right but he wants

move to another object (like a house or dungeon) that the player clicked on

avatar image g__l Ali-hatem · May 01, 2016 at 11:01 AM 1
Share

Thanks for letting me know, should have read the full question.

avatar image g__l · May 01, 2016 at 11:05 AM 0
Share

Not sure how it would work for the clicking on an object part, i guess you could get the click's x and y coordinates and compare them against the surrounding objects on the screens using (screen point to game point)

avatar image Ali-hatem g__l · May 01, 2016 at 11:19 AM 0
Share

by using Vector3 to be = to touch position & instead of HouseOrDungeon.transform.position the target will be that Vector3 with taking care of converting click position from pixels to points because clicks & touches defined by pixels where transform defined by points for example if we have 800 pixel screen width then click in the middle will bee 400 then you will send the transform 400 points imagine that so i think now i have to answer

avatar image

Answer by Ali-hatem · May 01, 2016 at 11:25 AM

 public float speed;
 void Update(){
     if (Input.GetMouseButton(0)){
        Vector3 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);//converting
         target.z = transform.position.z;
         transform.position = Vector3.MoveTowards(transform.position, target, speed);
     }
 }
Comment
g__l
Bruno85
Lazer989
zolutr
fateshow

People who like this

3 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 fateshow · Oct 25, 2020 at 12:40 PM 0
Share

Thank you, this helped me a lot.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

I need a script in C# which moves an object in only one direction at a time. 1 Answer

Unity2D moving on the y-axis 0 Answers

Player character becomes shaky when camera is moving 0 Answers

Make movement of an object independant. 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