Skip to content

Repository files navigation

Simple Unity Springs

Simple and free spring library for Unity, perfect for animating UI or game elements in your project, heavily inspired by https://www.react-spring.dev/.

Installation

Open your Unity package Manager, press "Add package from git url" and enter the following URL:

https://github.com/mashlol/SimpleUnitySprings.git

Usage

Basic usage

The Spring class lets you animate a single float value.

Movie_007.webm
usingSimpleUnitySprings;usingUnityEngine;publicclassMyBehaviour:MonoBehaviour{privateSpringspring;privatevoidStart(){spring=new(config:SpringConfig.Create(tension:600,friction:3),from:1,to:2f);}privatevoidUpdate(){spring.Tick(Time.deltaTime);transform.localScale=Vector3.one*spring.Get();}}

The Vector3Spring class lets you animate a Vector3 value.

Movie_006.webm
usingSimpleUnitySprings;usingUnityEngine;publicclassMyBehaviour:MonoBehaviour{privateVector3Springspring;privatevoidStart(){spring=new(config:SpringConfig.Create(tension:600,friction:30),from:newVector3(-6.5f,-2.2f,-2f),to:newVector3(6.5f,4f,-2f));}privatevoidUpdate(){spring.Tick(Time.deltaTime);transform.position=spring.Get();}}

Chaining

You can pass any Spring into ChainableSpring to support chaining, so each invocation of .To will queue until the previous has completed.

Movie_004.webm
usingSimpleUnitySprings;usingUnityEngine;publicclassMySpringChainingBehaviour:MonoBehaviour{// Initialize a new Vector3 spring starting at position 0,0,0 and moving to 1,1,1privateChainableSpring<Vector3>spring;privatevoidStart(){spring=new(newVector3Spring(config:SpringConfig.Create(tension:600,friction:30),from:newVector3(-6.5f,-2.2f,-2f),to:newVector3(6.5f,4f,-2f)));// One at a time the spring will transition to each destinationspring.To(newVector3(-9,5,0)).To(newVector3(4,-1,-4.6f)).To(newVector3(0,2f,-6.7f)).To(Vector3.zero);}privatevoidUpdate(){spring.Tick(Time.deltaTime);transform.position=spring.Get();}}

Spring Configs

A spring config tells the spring how to behave, and can either be created at runtime with SpringConfig.Create() or as a ScriptableObject in your project via Create -> Data -> SpringConfig.

It's recommended to use a ScriptableObject in your scene to share spring configs with multiple springs, as well as to be able to easily edit the configs at runtime to tweak values.

About

Simple and free spring library for Unity, perfect for animating UI or game elements in your project.

Resources

Stars

21 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages