- Home /
Lerping back and forth like a swipe page effect
Hi all, Im making a 2D game where the user is browsing through multiple shelves at a store and has button to so called "move" or "swipe" the shelf left or right when the user presses the left or right buttons. Im using C# Lerp and has made it that the shelves can be moved to the right once only and it will no longer be able to move anymore while the button to move the shelves to the left will continue to work.
Here's my code on this mechanic, you can take the code and try it for a clearer picture by creating 3 object where 1 of it will be the "shelves" while the other 2 will be the buttons for movement.
Thanks a lot in advance!
using UnityEngine; using System.Collections;
public class objectMovement : MonoBehaviour {
private Vector3 startMarker = Vector3.zero;
private Vector3 endMarker = Vector3.zero;
private float journeyLength;
private float startTime;
private float speed = 3f;
public Transform nextShelf;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (startMarker != Vector3.zero)
{
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
nextShelf.position = Vector3.Lerp(startMarker, endMarker, fracJourney);
}
}
void OnMouseDown()
{
//Move shelves to the left
if (tag == "browseNextShelf")
{
startTime = Time.time;
startMarker = nextShelf.position;
endMarker = nextShelf.position + new Vector3(10, 0, 0);
journeyLength = Vector3.Distance(startMarker, endMarker);
}
//Move shelves to the right
if (tag == "browsePreviousShelf")
{
startTime = Time.time;
startMarker = nextShelf.position;
endMarker = nextShelf.position - new Vector3(10, 0, 0);
journeyLength = Vector3.Distance(startMarker, endMarker);
}
}
}
Your answer
Follow this Question
Related Questions
MouseWheel Lerp Smoothing Problem 1 Answer
Object transported to point instead of moving towards it? 1 Answer
Stopping Vector3.Lerp 3 Answers
Define lerp axis through inspector 1 Answer
object movement as wave in Unity 3d 2 Answers