user9205278
user9205278

Reputation:

How to slow down rotation?

So I wrote some code to make an object rotate if I swiped left or right

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotater : MonoBehaviour {

public Transform player;

void Update()
{
    if (Input.touchCount == 1)
    {
        // GET TOUCH 0
        Touch touch0 = Input.GetTouch(0);

        // APPLY ROTATION
        if (touch0.phase == TouchPhase.Moved)
        {
            player.transform.Rotate(0f, 0f, touch0.deltaPosition.x);
        }

    }
}
}

and the problem is when I swipe fast the rotation will be uncontrollable. So I want the input to be less sensitive.

my goal is for the rotation to be like rolly vortex

my setup:

this setup made the player rotate in sort of an orbit which like I told you is similar to rolly vortex.

Upvotes: 2

Views: 592

Answers (2)

Tom Ryan
Tom Ryan

Reputation: 477

Rather than rotating by touch0.deltaPosition.x you could always have some sort of negative exponential function. In this case it’d probably be something along the lines of e^(-x-a) where x is your touch0.deltaPosition.x, and a would be a variable you’d have to determine based on how fast you want the initial speed of rotation. If you’re not familiar with exponential functions try using a graphing software like Desmos to plot y=e^(-x-a) and vary the value of a. Once you’ve visualised that it should be pretty self explanatory.

Upvotes: 1

Semimono
Semimono

Reputation: 724

First you want to be able to scale the sensitivity. This means making it so that for every unit of change in touch position, you will get some multiple of a unit of change in the rotation. For this, make a configurable (public) member variable, public float touchSensitivityScale, and multiply the rotation by that value. Example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotater : MonoBehaviour {

public Transform player;
public float touchSensitivityScale;

void Update()
{
    if (Input.touchCount == 1)
    {
        // GET TOUCH 0
        Touch touch0 = Input.GetTouch(0);

        // APPLY ROTATION
        if (touch0.phase == TouchPhase.Moved)
        {
            player.transform.Rotate(0f, 0f, touch0.deltaPosition.x * touchSensitivityScale);
        }

    }
}
}

Now you can edit the touch sensitivity in the inspector. With touchSensitivityScale set to 1, the behavior will be identical to what it is currently. If you make the number 0.5, the rotation will be half as sensitive.

If this doesn't fully solve the problem and you also want some smoothing or acceleration, that might warrant an edit to the question.

I hope it helps!

Upvotes: 1

Related Questions