Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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
Add comment · 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
3

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

Thank you, this helped me a lot.

avatar image
2

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
Add comment · 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 & ins$$anonymous$$d 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

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

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

Joystick problems 1 Answer

Question on Sprite and Movement 0 Answers

Simple C# script to move a gameObject toward the direction of the player at the instance of it's spawning. 1 Answer

Better way to move avatar than transform.position? 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