Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AlanParsons · Aug 17, 2012 at 09:32 PM · positiontouchdrag

Why does "draggable" MonoBehavior cause the object to be moved in the opposite direction?

I'm new to c#. I created this behavior that can be attached to a GameObject allowing the object to be moved according to Touch input position. I've followed many examples all which use this.transform.Translate() to translate the touch position to the object position. However, the object moves the opposite of the touch input. I've been reading (on answers and docs) and can't seem to figure out why.

 using UnityEngine;
 using System.Collections;
 
 public class DraggableBehavior : MonoBehaviour {
     
     public bool isSelected = false;
     public float speed = 0.1F;
     
     void Start () {
         
     }
     
     void Update () {
         
         if (Input.touchCount > 0) {
             
             Touch touch = Input.GetTouch(0);
             
             if (touch.phase == TouchPhase.Began) {
                 
                 Ray ray = Camera.main.ScreenPointToRay(touch.position);
                 RaycastHit hit ;
                 
                 if (Physics.Raycast (ray, out hit)) {
                     bool imSelected = (hit.transform.gameObject == this.gameObject);
                     if (this.isSelected == false && imSelected) {
                         this.isSelected = true;
                         Debug.Log(this.gameObject.name + " was selected");
                     } else {
                         this.isSelected = false;
                         
                     }
                 }
                 
             }
             
             if (touch.phase == TouchPhase.Moved && this.isSelected) {
                 Vector2 touchDeltaPosition = touch.deltaPosition;
                 transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
             }
             
         }
 
     }
     
 }

I'm using the iSelected property for another use. See the second if block in Update().

Comment
Add comment · Show 3
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 AlanParsons · Aug 17, 2012 at 09:48 PM 0
Share

Apples, thanks. That was one of the first things I tried but I think the result was that the object then didn't move. I will try again. Unity Remote is really acting up on me so this is taking forever.

avatar image AlanParsons · Aug 17, 2012 at 09:54 PM 0
Share

Apples, you were right. The movement is quite exaggerated in reference to the touch movement but at least it's right. Not sure why the examples in the docs have the values negative.

avatar image AlanParsons · Aug 17, 2012 at 10:13 PM 0
Share

I can't vote your comment best answer because it won't let me convert it.

1 Reply

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

Answer by Apples_mmmmmmmm · Aug 17, 2012 at 09:42 PM

for the line

transform.Translate(-touchDeltaPosition.x speed, -touchDeltaPosition.y speed, 0);

the negatives are most likely causing the opposite movement.

try

transform.Translate(touchDeltaPosition.x speed, touchDeltaPosition.y speed, 0);

instead.

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 DaveA · Aug 17, 2012 at 10:14 PM 0
Share

Please use 'answer' when you're answering. If asking for more info, that would be a comment.

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

10 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

Related Questions

How to instantiate object at touch position? 3 Answers

How to make object follow touch position?Pls Help 0 Answers

Drag Object, same speed as mouse/touch 3 Answers

How do I re-enable input after dragging off iPad screen? 0 Answers

How do you Draw a Line Using your Finger's Position on Android 3 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