- Home /
Help with refining rotation deciphering the switch and drag. This is for touch and C#.
Hey All!
First post here. I am creating a jar object and just like in real life you can rotate /spin the jar with your hands and stop it in place. This code works but I am having trouble making it look fluid and nice. I can clearly see the the switch between running switch and drag because it has a very hard stop once the distance was far enough. Any sense of direction or approach is greatly appreciated! I am a sponge and want to learn everything! Here is my pseudocode:
Pseudo
FixedUpdate()
addTorque to the rotation
For each touch:
TouchDown
Freeze the rotation. // when the finger is on the jar object, the object should stop rotating.
Initialize the touch. // needs to referenced to measure swipe.
TouchMove
if (swipe distance is greater than minimum swipe) // If swipe distance is greater than minimum swipe which is currently a test number. -----> then it is considered a swipe. Jar will rotate + the addTorque and to slow down rotation.
else ------> track the finger position and direction to drag and rotate the object.
TouchUp
Un-freeze the rotation
The touch from TouchDown is now a new Touch()
My Actual Code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ButtonCube : MonoBehaviour {
private Touch initialTouch = new Touch();
private float rotationRate = 5.0f;
private float amount = 5.0f;
private float torque = 3.0f;
public Rigidbody jar;
private float getXAxis;
private float distance;
private bool hasSwiped = false;
public Vector3 eulerAngleVelocity;
void FixedUpdate() {
if (Input.touchCount > 0) {
getXAxis = Input.touches[0].deltaPosition.x;
}
float h = getXAxis * amount * Time.deltaTime;
jar.AddTorque(transform.up * torque * -h);
foreach (Touch touch in Input.touches) {
Debug.Log ("Touching at: " + touch.position);
if (touch.phase == TouchPhase.Began) {
Debug.Log ("Freeze");
jar.freezeRotation = true;
initialTouch = touch;
}
else if (touch.phase == TouchPhase.Moved && !hasSwiped) {
Debug.Log ("Touch phase Moved");
float deltaX = initialTouch.position.x - touch.position.x;
distance = Mathf.Sqrt(Mathf.Pow(deltaX, 2));
if (distance > 50f) { // this is where I need HELP the most!
Quaternion deltaRotation = Quaternion.Euler (eulerAngleVelocity * Time.deltaTime);
jar.MoveRotation (jar.rotation * deltaRotation);
hasSwiped = true;
} else {
jar.transform.Rotate (0 * rotationRate, -touch.deltaPosition.x * rotationRate, 0, Space.World);
}
}
else if (touch.phase == TouchPhase.Ended) {
Debug.Log ("Touch phase Ended" + "UnFreeze");
jar.freezeRotation = false;
initialTouch = new Touch();
hasSwiped = false;
}
}
}
}
Thank you in advance! I am excited to hear your feedback.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113744im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Get Accelerometer to rotate 360 degrees 2 Answers
Distribute terrain in zones 3 Answers
GO will not rotate on swipe. 0 Answers
Turning touch to rotation 1 Answer