- Home /
Question by
Humansquirrel · Dec 07, 2021 at 04:21 AM ·
c#touch controls
drag to rotate cube
hi, here is the code how i rotate cube on drag but i want to rotate cube smoothly to 90 degrees or certain degree for every few units of drag like snap move.
public class PlatformRotate : MonoBehaviour
{
private Vector2 lastTapPos;
public bool CanRotate = true;
void Start()
{
}
public void StartRotate()
{
CanRotate = true;
}
void Update()
{
if (Input.GetMouseButton(0) && CanRotate)
{
Vector2 curTapPos = Input.mousePosition;
if (lastTapPos == Vector2.zero)
lastTapPos = curTapPos;
float delta = lastTapPos.x - curTapPos.x;
lastTapPos = curTapPos;
transform.Rotate(Vector3.forward * delta * 0.3f);
}
if (Input.GetMouseButtonUp(0))
{
lastTapPos = Vector2.zero;
}
}
Thank you
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613050303im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
TreeView and Touch Controls 0 Answers
Looking for an invisible button (that actually works!) 1 Answer