- Home /
help with movement along x axis and slow down. please help
Hello all,
I have this script attached to the camera in a 2D scene.
#pragma strict
private var screenPoint: Vector3;
private var offset: Vector3;
private var curScreenPoint : Vector3;
private var curPosition : Vector3;
var velocity : Vector3;
var underInertia : boolean;
var time : float = 0;
var SmoothTime : float;
var min : Vector3 = new Vector3(-9,0,0);
var max : Vector3 = new Vector3(75,0,0);
var within : boolean = false;
var frameVelocity : float;
function Start () {
}
function Update () {
if (transform.position.x > min.x && transform.position.x < max.x){
within = true;} else {within = false;}
if(underInertia && time <= SmoothTime && within == true)
{
transform.position += velocity;
velocity = Vector3.Lerp(velocity, Vector3.zero, time);
time += Time.smoothDeltaTime;
}
else
{
underInertia = false;
time = 0.0f;
}
if ( Input.GetMouseButtonDown(0)) {
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, 0, 0));
Screen.showCursor = false;
underInertia = false;
}
if ( Input.GetMouseButton(0) ) {
var prevposition : Vector3 = curPosition;
curScreenPoint = new Vector3(Input.mousePosition.x, 0, 0);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
velocity = curPosition - prevposition;
transform.position = curPosition;
}
if ( Input.GetMouseButtonUp(0)) {
underInertia = true;
Screen.showCursor = true;
}
}
This script keeps the camera's x motion inert in the direction of the mouse x position based on the speed of the swipe (either left or right). I am trying to slow down the inertia over time and stop motion of the camera, and also keep the motion/ movement only between the min and max variables in the script at all times.
I managed to make the inertia stop when mousebuttonup at the min and max points, but if i click and drag the motion continues past those points.
I am trying everything to fix these two problems, but I am a total noob, can anyone help with this? Any help will be greatly appreciated
I got the answer to limiting the motion between the two points which is by setting the transform position x to the $$anonymous$$ and max value if it is >< these values. Does anyone know how I can slow down the inertia on this script over time?
Answer by mealone · Sep 08, 2014 at 02:50 AM
Ok. I figured it out, just needed to change the 'time' var in the vector3.lerp to 0.05 (or whatever suits you if you use this script). Problem solved.