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 hollym16 · Dec 04, 2013 at 12:26 PM · c#vector3dragscroll

Touchscript drag constraints

I'm using a preset found here: https://www.assetstore.unity3d.com/#/content/7394 called Touchscript. Mainly the part that allows me to click and drag on Android devices. With this preset, the drag has a limit which is defined by the starting position of the GameObject (i.e. When you go into play mode it wont scroll down any further than the starting point) Looking into the (very complicated) set of code, I believe I've found the code that implements this:

 using System;
 using TouchScript.Events;
 using TouchScript.Gestures;
 using UnityEngine;
 using System.Collections;
 
 public class Side : MonoBehaviour
 {
 
     public float Speed = 10f;
 
     private Vector3 startPosition;
     private Vector3 targetPosition; 
 
     private void Start()
     {
         startPosition = targetPosition = transform.localPosition;
         GetComponent<PanGesture>().StateChanged += OnStateChanged;
     }
 
     private void Update()
     {
         //Debug.Log(targetY);
         var fraction = Speed * Time.deltaTime;
         //transform.localPosition = targetPosition;
         transform.localPosition = Vector3.Lerp(transform.localPosition, targetPosition, fraction);
     }
     private void OnStateChanged(object sender, GestureStateChangeEventArgs e)
     {
         switch (e.State)
         {
             case Gesture.GestureState.Began:
             case Gesture.GestureState.Changed:
                 var target = sender as PanGesture;
                 Debug.DrawRay(transform.position, target.WorldTransformPlane.normal);
                 Debug.DrawRay(transform.position, target.WorldDeltaPosition.normalized);
                 
                 var local = new Vector3(0, transform.InverseTransformDirection(target.WorldDeltaPosition).y, 0);
                 targetPosition += transform.parent.InverseTransformDirection(transform.TransformDirection(local));
 
                 if (transform.InverseTransformDirection(transform.parent.TransformDirection(targetPosition - startPosition)).y < 0) targetPosition = startPosition;
                 break;
         }
     }   
 }


Specifically this part:

     private Vector3 startPosition;
     private Vector3 targetPosition; 
 
     private void Start()
     {
         startPosition = targetPosition = transform.localPosition;
         GetComponent<PanGesture>().StateChanged += OnStateChanged;
     }

My question is, would anyone be able to adapt this code to apply to the bottom of the GameObject to prevent it scrolling up any further?

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
0
Best Answer

Answer by hollym16 · Dec 11, 2013 at 11:28 AM

I found out how to do it! After a week of confusion I worked out that in order to make it scroll to the bottom and not ping back up to the top, you must get the local position as you call the endPosition. This is the code I ended up with:

 using System;
 using TouchScript.Events;
 using TouchScript.Gestures;
 using UnityEngine;
 using System.Collections;
 
 public class Side : MonoBehaviour
 {
     public float Speed = 8f;
     private Vector3 startPosition;
     private Vector3 endPosition;
     private Vector3 targetPosition;
             
     private void Start()
     {
         startPosition = targetPosition = transform.localPosition;
         endPosition = targetPosition = transform.localPosition;
         GetComponent<PanGesture>().StateChanged += OnStateChanged;
     }
 
     private void Update()
     {
         var fraction = Speed * Time.deltaTime;
        transform.localPosition = targetPosition;
         transform.localPosition = Vector3.Lerp(transform.localPosition, targetPosition, fraction);
     }
     private void OnStateChanged(object sender, GestureStateChangeEventArgs e)
     {
         switch (e.State)
         {
             case Gesture.GestureState.Began:
             case Gesture.GestureState.Changed:
                 var target = sender as PanGesture;
                 Debug.DrawRay(transform.position, target.WorldTransformPlane.normal);
                 Debug.DrawRay(transform.position, target.WorldDeltaPosition.normalized);
                 
                 var local = new Vector3(0, transform.InverseTransformDirection(target.WorldDeltaPosition).y, 0);
                 targetPosition += transform.parent.InverseTransformDirection(transform.TransformDirection(local));
             
                  var end = new Vector3(0, transform.InverseTransformDirection(target.WorldDeltaPosition).y, 0);
                  targetPosition += transform.parent.InverseTransformDirection(transform.TransformDirection(end));
 
                 if (transform.InverseTransformDirection(transform.parent.TransformDirection(targetPosition - startPosition)).y < 0) targetPosition = startPosition;
             
                 if (transform.InverseTransformDirection(transform.parent.TransformDirection(targetPosition - endPosition)).y > 0.9) targetPosition = transform.localPosition; //Specifically specify here to find the local position, rather than referencing the endPosition
                 break;
             
         }
     }
 }
Comment
Add comment · 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

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Swapping 2 objects positions when one is dragged into the other 1 Answer

How do I get one object to point toward another using rigidbody physics? 2 Answers

Drag Object along local z-axis. Using WorldToScreenPoint. 1 Answer

Drag object using only keyboard in 2d sidescroller 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