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
0
Question by Jean-Fabre · Jan 06, 2011 at 02:03 PM · limitboundaryspringback

elastic scrolling scripting

Hi,

Where should I begin to script a value with elastic scrolling. something similar to the way iOs lets use drag a web page beyond the end and when you release spring back to its proper end ( hopefully that's clear enough description :) )

the goal is to drive a value, not necessarly an actual gui element, so that I can apply it to whatever.

I can make the value come back to its limit using some simple tween scripts but how to actually compute the value when the user drags further away from the boundary, it gets to a point where you can't drag it further, some kind of exponential math is involved, but my brain fried today :), and can't see the solution.

Thanks for your directions,

Jean

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 Jean-Fabre · Jan 07, 2011 at 07:50 AM 0
Share

found a good starting point: http://insideria.com/2010/09/elastic-scrolling-with-javascr.html

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by mtalbott · Dec 28, 2011 at 09:55 PM

This might be more than you're looking for but this has the "spring" at the end plus the "throwing" typical to iOS scrolling. I did not add the exponential fall off on the spring but that would be pretty easy to add. I think it feels fine without it anyway.

Add this to any GameObject and play. hope it helps:

 #pragma strict
 
 var spacer : int = 8;
 var barHeight : int = 32;
 var tiles : Tile[]; // home-made class. see the end of script.
 var springSpeed : float = 5.0; 
 var throwSpeed : float = 8.0;
 
 private var tileSize : int;
 private var screenWidth : int = Screen.width;
 private var screenHeight : int = Screen.height;
 private var slidePosition : Vector3;
 private var maxSlidePosition : Vector3;
 private var oldMousePosition : Vector3;
 private var throwPosition : Vector3;
 
 function Start () {
     // in a real senario maybe this is dynamically created from a database. for now, here are some blanks.
     tiles = [new Tile(), new Tile(),new Tile(),
             new Tile(), new Tile(), new Tile(),
             new Tile(), new Tile(), new Tile(),
             new Tile(), new Tile(), new Tile(),
             new Tile(), new Tile(), new Tile(),
             new Tile(), new Tile(), new Tile(),
             new Tile(), new Tile(), new Tile(),
             new Tile(), new Tile(), new Tile(),
             new Tile(), new Tile()];
     oldMousePosition = Input.mousePosition;
 }
 
 function Update () {
     if (!Input.anyKey) {
         if (slidePosition.x > 0){
             slidePosition = Vector3.Slerp(slidePosition, Vector3(0,0,0), Time.deltaTime * springSpeed);
         }
         else if (slidePosition.x < maxSlidePosition.x) {
             slidePosition = Vector3.Slerp(slidePosition, maxSlidePosition, Time.deltaTime * springSpeed);
         } 
         else {
              slidePosition = Vector3.Slerp(slidePosition, throwPosition, Time.deltaTime * springSpeed);
         }
         oldMousePosition = Input.mousePosition;
     }
     
     if (Input.GetButton ("Fire1")) {
         slidePosition.x += Input.mousePosition.x - oldMousePosition.x;
         throwPosition = Vector3(slidePosition.x+throwSpeed*(Input.mousePosition.x-oldMousePosition.x),0,0);
         oldMousePosition = Input.mousePosition;
     }
 }
 
 function OnGUI () {
     screenWidth = Screen.width;
     screenHeight = Screen.height;
     tileSize = (screenHeight-barHeight-(spacer*4))/3;
     
     var xMax : int = Mathf.CeilToInt(tiles.length/3.0f);
     maxSlidePosition.x = -spacer*(xMax+1)-tileSize*(xMax)+screenWidth;
     
     for (var i=0; i<tiles.length; i++){
         var position : float = i+1;
         var xGridPosition : int = Mathf.CeilToInt(position/3.0f);
         var yGridPosition : int = position - (xGridPosition-1)*3;
         var rect : Rect = new Rect(spacer*xGridPosition+tileSize*(xGridPosition-1)+slidePosition.x, spacer*yGridPosition+tileSize*(yGridPosition-1), tileSize, tileSize);
         var tile : Tile = tiles[i];
         GUI.Box(rect, tile.name);
     }
     GUI.Box(Rect(0, screenHeight-barHeight, screenWidth, barHeight), "Bottom Bar");
 }
 
 class Tile {
     var name : String = "name";
     var texture : Texture2D;
 }
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 IR_HellBlaDe · Aug 02, 2015 at 07:40 AM 0
Share

Thanks a lot you saved my life :D

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

1 Person is following this question.

avatar image

Related Questions

set limit mouse x y Help needed 1 Answer

All my variables are saying NaN. Am I overloading Unity? (solved) 0 Answers

C#:Unity:Having trouble reference Rigidbody2d from different script 2 Answers

How to make a limit so that my character stays on the screen? 0 Answers

making boundry for 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