N
N
Nikita Verdinsky2020-11-09 16:21:22
C++ / C#
Nikita Verdinsky, 2020-11-09 16:21:22

How to make smooth object rotation with fading (Unity 2D)?

Good afternoon ! I need your help. Tell me how I can implement it so that when I press the key, my object rotates along the z trajectory around its axis, but the rotation occurs smoothly with attenuation and only counterclockwise, as well as with an offset of 60-110 degrees from the current position on the z axis.

In my code, the rotation happens abruptly and on different sides.
The code:

spoiler

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

public class Player : MonoBehaviour
{
    public float RotatePlayer;
    public bool isTouchClick;

    private int min;
    private int max;

    void Start()
    {

    }

    void Update()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            isTouchClick = true;
        } 
        else
        {
            isTouchClick = false;
        }

        PlayerMove();
    }

    public void PlayerMove()
    {
        if (isTouchClick == true)
        {
            GetRandomRotation();
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, 0 + RotatePlayer), 0.07f);
        }
        else if (isTouchClick == false)
        {
            // transform.Rotate(0, 0, 0);
        }
    }

    public void GetRandomRotation()
    {

        min = 60;
        max = 110;

        Random rnd = new Random();
        RotatePlayer = Random.Range(min, max + 1);
    }   
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mopsicus, 2020-11-09
@mopsicus

For example, with AnimationCurve

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question