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 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.

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

Control key + A/D not working 3 Answers

How to remove blur when moving 2D objects 1 Answer

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

How to restrict AI from following the player on the Y-axis. (2D) 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