Unity中按⾃定义轴进⾏⾃转——Rotate浅谈
Rotate浅谈
最近做了太多关于⾃转的事情了,这下刚好弄个脚本,把情况都给总结⼀下,⾃⼰还可以⾃⼰选择⾃⼰想要的旋转轴,美滋滋,嘻嘻,对于⼀些⽐较刁钻的⾓度的花就需要⾃⼰在脚本上⾃⼰输⼊⼀下啦
废话不多说,上代码·······
代码清晰易懂,没有写太多注释
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum TypeAxis
{
x, y, z, outer, Middle,Inside,Custom
}
///
/// 根据哪个轴⾃转
///
public class RotateSelf : MonoBehaviour {
private Quaternion RotatePos;
public float SpeedStart;
public float SpeedBack;
public TypeAxis TA = TypeAxis.x;
public int X, Y, Z;
public bool SelfRotate = true;
void Awake()
{
RotatePos = transform.localRotation;
}
// Update is called once per frame
void FixedUpdate () {
if (SelfRotate == true)
{
switch (TA)
{
ca TypeAxis.x:
transform.Rotate(new Vector3(1, 0, 0) * Time.deltaTime * SpeedStart);
break;
ca TypeAxis.y:
transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * SpeedStart);
break;
ca TypeAxis.z:
transform.Rotate(new Vector3(0, 0, 1) * Time.deltaTime * SpeedStart);
break;
ca TypeAxis.outer:
transform.Rotate(new Vector3(-1, -1, -1) * Time.deltaTime * SpeedStart);
break;
ca TypeAxis.Middle:
transform.Rotate(new Vector3(-1, -1, 1) * Time.deltaTime * SpeedStart);
break;
ca TypeAxis.Inside:
transform.Rotate(new Vector3(1, 1, 1) * Time.deltaTime * SpeedStart);
break;
ca TypeAxis.Custom:
transform.Rotate(new Vector3(X, Y, Z) * Time.deltaTime * SpeedStart);
break;
default:
break;
}
}
el
{
Quaternion NewAngle = transform.localRotation ;
Quaternion p = Quaternion.Lerp(NewAngle, RotatePos,Time.deltaTime * SpeedBack); transform.localRotation = p;
}
}
}