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

More than 3 years have passed since last update.

posted at

updated at

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

こちら結論から言ってしまうと`Transform.RotateAround(Vector3 point, Vector3 axis, float angle);`を使う事で実装する事が出来た。

Transform.RotateAround

こちらを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)
{
if (nowAngle <= RotatingAngle)
{
}
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?