using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
enum Axistorotate { Back, Down, Forward, Left, Right, Up, Zero};
public float angle;
public float speed;
public Vector3 axis;
private bool stopRotation = true;
private Axistorotate myAxis;
// Start is called before the first frame update
void Start()
{
myAxis = Axistorotate.Left;
StartCoroutine(RotateObject());
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.S))
{
stopRotation = false;
}
if (Input.GetKeyDown(KeyCode.C))
{
stopRotation = true;
StartCoroutine(RotateObject());
}
}
IEnumerator RotateObject()
{
while (stopRotation == true)
{
transform.Rotate(Axistorotate.Left, angle);
yield return new WaitForSeconds(speed);
}
}
}
在线:
transform.Rotate(Axistorotate.Left, angle);
我想使用枚举或做一些我可以在Vector3.Left或Vector3.Right之间进行选择的...以及所有像枚举一样简短的轴。
想法是能够在此单行而不是多行中简短地使用枚举或vector3轴。
答案 0 :(得分:0)
仅使用Vector3
常量并将它们分配给Vector3
变量可能是最简单的。但是,有时您又想使用类型安全性来确保您的方法不会收到意外的轴或Vector3.zero
。
您不能仅使用enum
来完成此操作(您需要使用开关或数组或其他方式将enum
解释为Vector3
),但是您可以通过具有私有构造函数,一些只读字段和public static implicit operator Vector3
方法的类,可以得到类似且自成体系的东西:
public class RotationAxis
{
public static readonly AxisVector right = new Axis(Vector3.right);
public static readonly AxisVector left = new Axis(Vector3.left);
public static readonly AxisVector up = new Axis(Vector3.up);
public static readonly AxisVector down = new Axis(Vector3.down);
public static readonly AxisVector forward = new Axis(Vector3.forward);
public static readonly AxisVector back = new Axis(Vector3.back);
private Vector3 _value;
private RotationAxis(Vector3 axis)
{
this._value = axis;
}
public static implicit operator Vector3(RotationAxis axis)
{
return axis._value;
}
}
使用方法示例:
public void RotateMe(RotationAxis axis, float angle) {
// using axis as a Vector3 guaranteed by type safety to be one of:
// Vector3.right/left/up/down/forward/back
// RotationAxis implicitly converts to Vector3
transform.Rotate(axis, angle);
// you can also just use RotationAxis._ as you would Vector3._
transform.Rotate(RotationAxis.forward, angle);
}
...
RotationAxis axis = RotationAxis.right;
float angle = 45f;
object1.RotateMe(axis, angle);
axis = RotationAxis.up;
angle = 45f;
object2.RotateMe(axis, angle);
// object2.RotateMe(Vector3.zero, angle); would produce a compile error. Can't be done accidentally.
答案 1 :(得分:0)
实际上,您可以根据需要使用枚举。您可以进行如下操作:
public class Rotate : MonoBehaviour
{
public enum Axistorotate { Back, Down, Forward, Left, Right, Up, Zero };
public Vector3[] vectorAxises = new Vector3[7];
public Axistorotate myAxis;
然后从开始:
void Start()
{
vectorAxises[0] = Vector3.back;
vectorAxises[1] = Vector3.down;
vectorAxises[2] = Vector3.forward;
vectorAxises[3] = Vector3.left;
vectorAxises[4] = Vector3.right;
vectorAxises[5] = Vector3.up;
vectorAxises[6] = Vector3.zero;
然后创建一个函数:
public Vector3 GetAxis(Axistorotate axis)
{
return vectorAxises[(int)axis];
}
然后像这样使用它:
transform.Rotate(GetAxis(Axistorotate.Left), angle);
或
transform.Rotate(GetAxis(myAxis), angle);