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 Bentoon · Aug 26, 2014 at 08:53 PM · touchscrollmomentum

Touch Scrolling momentum

Hello everyone

I have been using a .js touch script for use with a sidescroller and it works lovely.

(Script Below)

But now I want to add momentum after the user lifts their finger... Any Ideas?

 #pragma strict
 
 // Moves object according to finger movement on the screen
 
     var speed : float = 0.05;
     var nonSpeed : float = 0;
     
     function Update () {
         if (Input.touchCount > 0 && 
           Input.GetTouch(0).phase == TouchPhase.Moved) {
         
             // Get movement of the finger since last frame
             var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
             
             // Move object across XY plane
             transform.Translate (-touchDeltaPosition.x * speed,
             0,0);
             // -touchDeltaPosition.y * nonSpeed,0);             
         
         }
     }


Thanks Y'all

~be

Comment
Add comment · Show 1
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 Bentoon · Sep 23, 2014 at 04:35 AM 0
Share

Anyone? Here is a script that uses the Left & Right Arrow $$anonymous$$eys (as well as touch) so you can test

 #pragma strict
 
 // $$anonymous$$oves object according to finger movement on the screen
 
     var speed : float = 0.05;
     var nonSpeed : float = 0;
     
     function Update () {
 
     // For arrow keys:
               if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow) ){
           Debug.Log("right");
             transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
           }
                     
                 if ( Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow) ){
                           Debug.Log("left");
             transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
           }
 
     // For Touch
         if (Input.touchCount > 0 && 
           Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
         
             // Get movement of the finger since last frame
             var touchDeltaPosition:Vector3 = Input.GetTouch(0).deltaPosition;
             
             // $$anonymous$$ove object across X plane
             transform.Translate (-touchDeltaPosition.x * speed,
             0,0);
             
             //transform.Translate (-touchDeltaPosition.z * speed,
             //0,0);
             
         //}
     //}


Thanks!

I'd actually love to do this in C#

~Be

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cruisereric · Jan 12, 2018 at 09:53 PM

Although it's been a little late for the answer to be given but for pple who had same problem (I know that the original question was in javascript but Im using c#, so excuse me), I've had the same problem and I end up adding rigid body to my object and then a Constant Force component to it. then In the rotation code, I called a CoRoutin in which I assign a value to Y part of relative torque of the constant force component and in some seconds I set it from 4 to 0.

  IEnumerator slowDown(bool forward /*check if the movement is in the positive direction or negative direction*/){
                     //the initial value of relative torque's Y
             speed = 4;
             while (speed > 0) {
                 speed -= 0.5f;
           
 //cf has been assigned in Start function to be the Constant Force component of the game object
                 cf.relativeTorque = new Vector3(0f,forward?speed:-speed,0f);
     yield return new WaitForSecondsRealtime (1f);
             }
                  // we make sure everything is 0 after the while loop has been finished
             cf.relativeTorque = new Vector3(0f,0f,0f);
             yield return new WaitForSecondsRealtime (0f);
     
         }
 

It is working good in my case and you may need to add more logic or change default values to better tune it with your environment.

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

23 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

Related Questions

How to draw a line with touch? 1 Answer

Touch movement not working 1 Answer

EARTH CALLING ANSWER!!!! 1 Answer

Camera momentum after lifting finger 0 Answers

Inertia Scrolling Touch 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