0
\$\begingroup\$
public class PlayerControl : MonoBehaviour
{
    float Speed = 125f;
    void FixedUpdate()
    {
        transform.Rotate(0, 0, Speed * Time.deltaTime);
    }

    public void ChangeDirection()
    {
        Speed = -Speed;
    }

    public void PauseMovement() <---- Pointer Down
    {
        Speed = 0f;
    }

    public void Boost()
    {
        Speed = 330f * Mathf.Sign(Speed);
    }

    public void NormalSpeed()   <----- Pointer Up
    {
        Speed = 125f * Mathf.Sign(Speed);
    }

}

I press a button on a trigger event as pointer down, my player stops moving and when i release the button, it resumes as per normal. This works but not as how i intended, when i want my player to resume, i want it to resume the direction it last went. Like if it rotates to the left, it will continue on the left. Right now, it last rotates on the left, it continues on the right. Please advice?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ How about instead of setting speed to 0, you introduce a pause bool. That way you can either add a condition in your FixedUpdate to only Rotate when you are not paused or multiply it in your rotate function (0 for paused, 1 for not paused). \$\endgroup\$
    – Zibelas
    Apr 13 at 9:32

1 Answer 1

1
\$\begingroup\$

First I will define a bool:

private bool IsClockwise; // set it to true or false depending on your need

And then making the Speed variable non-negative:

    float Speed = 125f;
    void FixedUpdate()
    {
        transform.Rotate(0, 0, Speed * Time.deltaTime);
    }

    public void ChangeDirection()
    {
        // We don't change the value of Speed here!
    }

    public void PauseMovement() <---- Pointer Down
    {
        Speed = 0f;
    }

    public void Boost()
    {
        Speed = 330f;
    }

    public void NormalSpeed()   <----- Pointer Up
    {
        Speed = 125f;
    }

And finally, let's define the new ChangeDirection method:

public void ChangeDirection()
{
    IsClockwise = !IsClockwise;
}

But it won't affect anything before we tweak the rotation part:

void FixedUpdate()
{
    if (IsClockwise)
        transform.Rotate(0, 0, Speed * Time.fixedDeltaTime);
    else
        transform.Rotate(0, 0, -Speed * Time.fixedDeltaTime);
}

Note that as you want to do this in FixedUpdate, you will need to use fixedDeltaTime instead of deltaTime (see Unity's documentation)

\$\endgroup\$
4
  • 1
    \$\begingroup\$ You do not need to use Time.fixedDeltaTime just because it is FixedUpdate(). Time.deltaTime automatically uses fixedDeltaTime if called from FixedUpdate(). docs.unity3d.com/ScriptReference/Time-deltaTime.html \$\endgroup\$
    – Zibelas
    Apr 13 at 19:31
  • \$\begingroup\$ @Zibelas thanks, I didn't know that. Though I think writing fixedDeltaTime is more clear (just a personal preference) \$\endgroup\$
    – Arian_ki
    Apr 13 at 19:40
  • \$\begingroup\$ thank you so much! \$\endgroup\$ Apr 17 at 9:53
  • \$\begingroup\$ @ShihsShihis If this answer helped you fix the issue, please consider marking it as "accepted". \$\endgroup\$
    – Vaillancourt
    May 13 at 18:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .