Hey guys, I amhaving some issues with a script I have put together through some research online. I have the script working, however there are a few bugs. I want to swipe as well as drag on the screen and have the object rotate freely. So if i swipe and remove my finger the object should freely spin but its speed should decay until it stops.
Right now if I swipe or drag it rotates buy it doesnt lerp down to a stop and it rotates on multiple axis even though I have set it to use 1. Here is my code:
public enum Axes {up, forward, right};
public enum Rotations {left, right};
public Axes ObjAxis;
public Rotations Direction;
private float dir;
private Vector3 axis;
Camera referenceCamera;
bool dragging;
Vector3 speed;
Vector3 avgSpeed;
float i;
float lerpSpeed;
float rotationSpeed;
public Vector2 TouchPosStart;
public Vector2 TouchPosEnd;
Touch oldMousePos;
public Vector2 curMousePos = Vector2.zero;
Vector2 mouseDif = Vector2.zero;
private float mouseXAmount, mouseYAmount;
//initialization
private Vector3 initialRotation;
void Awake() {
initialRotation = gameObject.transform.localEulerAngles;
}
void Start ()
{
i = 0;
lerpSpeed = 2.2f;
rotationSpeed = 0.05f;
dragging = false;
}
void OnEnable() {
gameObject.transform.localEulerAngles = initialRotation;
}
void Update () {
//allows changing axss in editor
if(Application.isEditor)
{
if(Direction == Rotations.left)
{
dir = -1;
}
else
{
dir = 1;
}
switch(ObjAxis)
{
case Axes.up:
axis = gameObject.transform.up;
break;
case Axes.forward:
axis = gameObject.transform.forward;
break;
case Axes.right:
axis = gameObject.transform.right;
break;
}
}
if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch (0);
switch (touch.phase)
{
case TouchPhase.Began:
//we clicked, so ignore drag for now
dragging = false;
// Get the current mouse position
TouchPosStart = touch.position;
break;
case TouchPhase.Moved:
// Get the difference between the last frame's position and this frame's position
// This is your delta
TouchPosEnd = touch.position - TouchPosStart;
// Set the oldMousePos to the current value for use in the next frame
TouchPosStart = touch.position;
break;
case TouchPhase.Stationary :
dragging = true;
break;
default:
break;
}
}
/*
if(Input.GetMouseButtonDown(0)) {
//we clicked, so ignore drag for now
dragging = false;
}
else if (Input.GetMouseButton (0)) {
dragging = true;
} else {
dragging = false;
}
*/
if (dragging)
{
//calculate speed based on magnitude of mousedif
speed = new Vector3 (TouchPosEnd.x, -TouchPosEnd.y, 0);
//average the new/old speed, and add a time multiplication factor to increase the duration of effect
avgSpeed = Vector3.Lerp (avgSpeed, speed, Time.deltaTime * 5);
}
else
{
if (dragging)
{
speed = avgSpeed;
dragging = false;
}
//lerp speed down to 0 because we stopped dragging
i = Time.deltaTime * lerpSpeed;
speed = Vector3.Lerp(speed, Vector3.zero, i);
}
//rotate the object around axis, setting speed and spin direction
transform.Rotate( axis * speed.x * rotationSpeed * dir, Space.World );
}