11
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

Cubeを接地した状態で回転させる方法について

少し前にTwitterで「箱を角が設置した状態で回すのってどうやってるのか?」と言う書き込みを見て、
自分自身も少し気になってきたので調べてみた。

実装するにあたっては回転軸の位置を指定しながら回転させる必要があるが、
こちら結論から言ってしまうとTransform.RotateAround(Vector3 point, Vector3 axis, float angle);を使う事で実装する事が出来た。

Transform.RotateAround

今回テストとして実装した物としては以下の様な挙動となる。

Rotate.gif

実装時のテストコードはこちら。
こちらをCubeにアタッチする事で動作可能。

using System.Collections;
using UnityEngine;

public class Test : MonoBehaviour
{
    const float RotatingSpeed = 0.2f;
    const float RotatingAngle = 90f;
    Vector3 halfSize;
    float time = 0f;
    Vector3 axis = Vector3.zero;
    Vector3 point = Vector3.zero;

    void Awake()
    {
        this.halfSize = this.transform.localScale / 2f;
    }

    void Update()
    {
        if (this.point != Vector3.zero) { return; }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            this.axis = Vector3.right;
            this.point = this.transform.position + new Vector3(0f, -this.halfSize.y, this.halfSize.z);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            this.axis = Vector3.left;
            this.point = this.transform.position + new Vector3(0f, -this.halfSize.y, -this.halfSize.z);
        }
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            this.axis = Vector3.forward;
            this.point = this.transform.position + new Vector3(-this.halfSize.x, -this.halfSize.y, 0f);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            this.axis = Vector3.back;
            this.point = this.transform.position + new Vector3(this.halfSize.x, -this.halfSize.y, 0f);
        }
        if (this.point != Vector3.zero)
        {
            StartCoroutine(this.StartRotate());
        }
    }

    IEnumerator StartRotate()
    {
        float nowAngle = 0f;
        float addAngle = (RotatingAngle / (RotatingSpeed * 60f));
        while (this.time <= RotatingSpeed)
        {
            nowAngle += addAngle;
            if (nowAngle <= RotatingAngle)
            {
                this.transform.RotateAround(this.point, this.axis, addAngle);
            }
            this.time += Time.deltaTime;
            yield return null;
        }
        this.time = 0f;
        this.axis = this.point = Vector3.zero;
        yield break;
    }
}

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
11
Help us understand the problem. What are the problem?