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 Kk1496 · May 03, 2017 at 03:06 AM · 2dspritemousedragdrag-and-drop

Drag and Drop w/ Snapping

I have a sprite that can be clicked and dragged around the screen. The sprites need to be placed in such a way that they connect. I've scaled the sprites so that the objects can snap if they are moving in steps of 0.25

The script seems to check that the mouse has moved the correct distance once and continues to move the objet.

 using System.Collections;
 using System.Collections.Generic;
 
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Pipe : MonoBehaviour {
 
     private const float X_BOUND = 4.4f;
     private const float Y_BOUND = 4.0f;
     private const float STEP = 0.25f;
     private const float STEP_TEST = 1.5f;
 
     private bool isSelected;
     private Vector3 lastMousePos;
 
     private void Update () {
 
         if (isSelected) {
 
             Vector3 newPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
 
             float yDist = newPos.y - lastMousePos.y;
             float xDist = newPos.x - lastMousePos.y;
 
             if ((STEP_TEST <= Mathf.Abs (xDist)) || (STEP_TEST <= Mathf.Abs (yDist))) {
 
                 print (Time.deltaTime);
                 print ("last: " + lastMousePos + " New: " + newPos);
 
                 if (newPos.y < lastMousePos.y) {
 
                     transform.position += Vector3.down * STEP;
                 
                 } else if (newPos.y > lastMousePos.y) {
 
                     transform.position += Vector3.up * STEP;
                 
                 } 
 
                 if (newPos.x < lastMousePos.x) {
 
                     transform.position += Vector3.left * STEP;
                 
                 } else if (newPos.x > lastMousePos.x) {
 
                     transform.position += Vector3.right * STEP;
                 }
 
                 lastMousePos = newPos;
 
             }
             
         }
 
         Vector3 clamped = new Vector3 (
                               Mathf.Clamp (transform.position.x, -X_BOUND, X_BOUND),
                               Mathf.Clamp (transform.position.y, -Y_BOUND, Y_BOUND)
                           );
 
         transform.position = clamped;
 
     }
         
     private void OnMouseDown () {
 
         isSelected = true;
         PipeManager.selected = this.transform;
     }
 
     private void OnMouseUp () {
 
         isSelected = false;
         PipeManager.selected = null;
     }
 
     public void Snap () {
         
     }
 
 }

Project on GitHub

Comment
Add comment · Show 2
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 jdean300 · May 03, 2017 at 05:05 AM 1
Share

Don't know if this is the cause, but this is suspicious: float xDist = newPos.x - last$$anonymous$$ousePos.y;. That should probably be last$$anonymous$$ousePos.x

avatar image Kk1496 · May 03, 2017 at 04:08 PM 0
Share

The Sprite moves more s$$anonymous$$dily. However, the mouse cursor seems to lag behind in some cases. Is there a better way to go about doing this?

0 Replies

· Add your reply
  • Sort: 

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

121 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

Related Questions

Dragging one object, but when it overlaps another it sticks also 0 Answers

Display sprite where clicked on screen. 1 Answer

[2D Mode] Trying to make a sprite dragable 0 Answers

How do I drag a sprite around? 2 Answers

Mouse Click Based Movement/Actions & 2D Sprite Rotation in 3D Space 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