ブログ

オブジェクトを指定角度まで滑らかに回転させる方法(Rotate/RotateTowards)

 

(その1)

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

public class MoveRotate : MonoBehaviour {

    private Vector3 pos;
    private Quaternion rot;

    void Update () {

        pos = transform.position;
        rot = transform.rotation;

        print(rot.z);

        if(pos.y < 5.1f) { transform.Translate(Vector3.up * Time.deltaTime); } if (pos.y > 5.0f)
        {
            Invoke("BRotate", 0.5f);
        }
	}

    void BRotate()
    {
        if (rot.z > -0.22f)
        {
            transform.Rotate(new Vector3(0, 0, -10) * Time.deltaTime);
        }
    }
}

(その2)

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

public class MoveRotate : MonoBehaviour
{
    private Vector3 pos;

    void Update()
    {
        pos = transform.position;
        
        if (pos.y < 5.1f) { transform.Translate(Vector3.up * Time.deltaTime); } if (pos.y > 5.0f)
        {
            Invoke("BRotate", 0.5f);
        }
    }

    void BRotate()
    {
        transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, 0, -25), 0.3f);
    }
}