'How can I use enum with Vector3 axis?
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);
}
}
}
At the line :
transform.Rotate(Axistorotate.Left, angle);
I want to use the enum or to make something that I will be able to select between Vector3.Left or Vector3.Right....And all axis in short like enum.
The idea is to be able to use the enum or the vector3 axis in short at this single line instead many lines.
Solution 1:[1]
You actually can do it with enums, like you want. You can make something like this:
public class Rotate : MonoBehaviour
{
public enum AxisToRotate { Back, Down, Forward, Left, Right, Up, Zero };
static readonly Vector3[] vectorAxes = new Vector3[] {
Vector3.back,
Vector3.down,
Vector3.forward,
Vector3.left,
Vector3.right,
Vector3.up,
Vector3.zero
};
public AxisToRotate myAxis;
Then make a function:
public Vector3 GetAxis(AxisToRotate axis)
{
return vectorAxes[(int)axis];
}
And then use it like this:
transform.Rotate(GetAxis(AxisToRotate.Left), angle);
or
transform.Rotate(GetAxis(myAxis), angle);
Solution 2:[2]
It's probably easiest to just use the Vector3 constants and assign them to a Vector3 variable. But then again sometimes you want to use type safety to ensure that your method can't receive an unexpected axis or Vector3.zero.
You can't do it only with an enum (you'd need a switch or an array or something else to interpret the enum as a Vector3) , but you can get something similar and self-contained with a class with a private constructor, some readonly fields, and a public static implicit operator Vector3 method:
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;
}
}
Example of how to use:
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DMGregory |
| Solution 2 |
