Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by zombiewalker · Mar 13, 2012 at 05:57 AM · swipesnapslide

How to create "Swipe-Page" or "Slide-Page" effects?

I am trying to create some functions for an eBook(not flipping animation): 1.User finger-swipe the page to the left 2.Page slides to the left 3.Then the page "snap" to fit the screen (or snap to a specific position) with ease out animation

Here is the example I would like to achieve: sliding page example

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 tannaz-rashidi · Aug 21, 2017 at 04:31 AM 0
Share

could You please to post your script that create this sliding page example ? thank U so mush

2 Replies

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

Answer by save · Mar 13, 2012 at 09:48 AM

You would use Input.GetTouch to see the deltaPosition and Mathf.Lerp to lerp towards a desired goal.

To move something you can use the deltaPosition:

if (Input.touchCount>0 && Input.GetTouch(0).phase==TouchPhase.Moved) {
    var touchDelta : Vector2 = Input.GetTouch(0).deltaPosition;
    transform.Translate (-touchDelta.x, 0, 0);
}

To see if the user stopped giving input and see if it was a flick:

if (Input.touchCount>0 && Input.GetTouch(0).phase==TouchPhase.Ended) {
    var touchDelta : Vector2 = Input.GetTouch(0).deltaPosition;
    if (touchDelta.x>5) {} //This was a flick to the left with magnitude of 5 or more
    if (touchDelta.x<-5) {} //This was a flick to the right with magnitude of 5 or more
}

To set the goal you'd either use a list of values or something like:

goal = Mathf.Round(transform.position.x/goalGrid)*goalGrid; //Where goalGrid would be an int of steps

Then lerp towards that goal:

transform.position.x = Mathf.Lerp(transform.position.x, goal, speed*Time.deltaTime); //Where speed would be the multiplier to reach into position

You would also want to clamp that goal so it never exceed the limits of the present world.

goal = Mathf.Clamp(goal, minimum, maximum);

You have to implement this into your system and specialize it for your needs, although this should be the only logic you'll need to make it swipe and flick. Preferably you'd create functions where you'd check what phase the touch is in, if Input.touchCount==0 you'd initiate the lerp towards the goal.

Comment
Add comment · Show 4 · 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 save · Mar 13, 2012 at 10:17 AM 0
Share

Examples will update soon /$$anonymous$$r Edit..

avatar image zombiewalker · Mar 23, 2012 at 11:55 AM 0
Share

This clever line saves me lots of time: goal = $$anonymous$$athf.Round(transform.position.x/goalGrid)*goalGrid; Everything works, except: 1.game always needs to wait about 10 seconds, then the page can "snap" to the goal position. 2.I am using " transform.position = Vector3.Lerp(transform.position, new Vector3(goalPos, 0, 0) , Time.time*smoothSpeed); " But if I put it inside "if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)" , the "damp" animation won;t work. Still no ideas what's going on...

But your code really saved my life!

avatar image infinitypbr · Mar 02, 2013 at 03:30 AM 0
Share

This is helpful code.

avatar image game-dev333 · Feb 15, 2014 at 06:04 AM 0
Share

Everything works fine, but limit is not effecting at all.

why is it so?

avatar image
0

Answer by zombiewalker · Mar 14, 2012 at 08:31 AM

Thanks for replying so fast! I am digesting your solution now. But I met a problem when I am getting touchDelta value (to see if an user stopped/lifted his finger). Here are my code, in my idea, it's supposed emit some particles, but it didn't response anything without any errors:

pragma strict

 var fx1: GameObject;
 var fx2: GameObject;
 
 function Start () {
 
 }
 
 function Update () 
 {
     if (Input.touchCount>0 && Input.GetTouch(0).phase==TouchPhase.Ended) 
     {
         var touchDelta : Vector2 = Input.GetTouch(0).deltaPosition;
         if (touchDelta.x >5) 
         {
             Instantiate(fx1, transform.position, Quaternion.identity);
         
         } //This was a flick to the left with magnitude of 5
         if (touchDelta.x <-5) 
         {
             Instantiate(fx2, transform.position, Quaternion.identity);
         } //This was a flick to the right with magnitude of 5
     }
 }

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I fix my Swiping Down Errors? 1 Answer

camera slides and bounces while moving C# 1 Answer

Swipe and Joystick Together on Mobile 0 Answers

How can I make this "swipe w/ force"? Camera tilt/pan on swipe. 1 Answer

swipe repeating more then once 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