• 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 EvanCheddar · Apr 09, 2018 at 09:17 PM · movement2d gamemouseclickmouse-dragy-axis

How can I move my 2D main player up and down by clicking and dragging him up or down?

Hello,

What I want to do is so simple in theory but not working in practice. I have a 2D game with a player that can only move along the Y-axis (up and down). Right now he moves up and down with the 'W' and 'S' keys but I want him to move up and down with a mouse click and drag up or down. Thank you in advance for anyone who can help.

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

1 Reply

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

Answer by davidcox70 · Apr 09, 2018 at 11:34 PM

Add this to your object, ensuring it has a collider (which is needed to receive the mouse event);

 //
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;    
 
 public class dragMe : MonoBehaviour {
 
     private Vector3 screenPos;
     private Vector3 offset;
 
     void OnMouseDown(){
         // work out the target object is on the screen
         screenPos = Camera.main.WorldToScreenPoint (transform.position);
 
         //    store the starting position of the drag
         offset=transform.position-Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,screenPos.z));
 
     }  
 
     void OnMouseDrag(){

// convert just the Y position Vector3 currentScreenPos = new Vector3 (screenPos.x, Input.mousePosition.y, screenPos.z);

// add the resulting tranform transform.position = Camera.main.ScreenToWorldPoint (currentScreenPos) + offset;

     }
 }
Comment
Add comment · Show 6 · 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 EvanCheddar · Apr 10, 2018 at 02:01 AM 0
Share

Hey thank you for your time! So I tried your suggested code and it works except that when I click on my player object, his transform shifts and he gets moved to the left and also pushed back on the z axis. The mouse drag works and I'm able to move him up and down which is great, but is there a way I can keep the players transform the same?

avatar image EvanCheddar · Apr 10, 2018 at 02:26 AM 0
Share

i fixed it so that on mouse click doesn't change the transform, however, when I drag the game object up and down, I notice that the players Z transform is changing as well as the Y's. I cant seem to get just the Y transform to change

avatar image davidcox70 EvanCheddar · Apr 10, 2018 at 10:06 AM 0
Share

Hello, It shouldn't change the Z position. But just to make sure, you could make this change. Change the line:

transform.position = Camera.main.ScreenToWorldPoint (currentScreenPos) + offset;

to:

 Vector3 newPos=Camera.main.ScreenToWorldPoint (currentScreenPos) + offset;
 
 transform.position = new Vector3(transform.position.x,newPos.y,transform.position.z);
 

This means only the Y position of the transform is changed. X and Z are copied.

avatar image EvanCheddar davidcox70 · Apr 10, 2018 at 03:20 PM 0
Share

That solved the issue I was having. Thank you so much for taking the time to help me out. I really appreciate it!

Show more comments

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

2d movement 1 Answer

2D Game, Player Not to move if a wall is next to it C# 1 Answer

Movement looks “choppy” on iPhone 1 Answer

2D Character Jumps only 1 time 2 Answers

calling setTile(location, null) freezes game for half a second 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges