Skip to content

Instantly share code, notes, and snippets.

@mao-test-h
Created June 4, 2017 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mao-test-h/42d580978b5fabccbe4a6a0e12b58f32 to your computer and use it in GitHub Desktop.
Save mao-test-h/42d580978b5fabccbe4a6a0e12b58f32 to your computer and use it in GitHub Desktop.
Cubeを接地した状態で回転させる方法について
using System.Collections;
using UnityEngine;
public class RotateCubeTest : 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment