unity中实现物体在⼀定⾓度范围内来回旋转
1using System.Collections;
2using System.Collections.Generic;工藤新一用日语怎么说
3using UnityEngine;
4
5public class Rotate : MonoBehaviour {
6private float origionZ;展望未来
7private Quaternion targetRotation;
8public float RotateAngle = 60;
9public int count = 0;
amending
10private bool i;
11// U this for initialization
12void Start () {gook
13 origionZ = ation.z;
14 }
15
16// Update is called once per frame
17void Update () {
18if (Input.GetKeyDown(KeyCode.D))//当按下D时进⾏旋转
19 {
20if (count >= 3)
21 {
22 i = fal;
23 }
24if (count <= 0)
25 {
26 i = true;
27 }
28
29if (i == true)
30 {
31 count++;
32 targetRotation = Quaternion.Euler(0, 180, RotateAngle * count + origionZ) * Quaternion.identity;
33 }
34if(i==fal)
twitter 中文35 {
36 count--;
37 targetRotation = Quaternion.Euler(0,180,RotateAngle*count+origionZ) * Quaternion.identity;
38 }
39
hfut40
41 }
42el
43 {
44 ation = Quaternion.ation, targetRotation, Time.deltaTime * 2);
45//避免误差
46if (Quaternion.Angle(targetRotation, ation) < 1)
47 ation = targetRotation;
48 }
49
上海文来中学九月的英文50 }
51 }
使⽤四元数可以避免万向锁的问题,并且实现平滑转化。当按下D键时,物体的z轴会旋转60度,在该脚本中,物体的Z轴在0~180度之间来回变化,其中count的值可以改变,造成的结果就是⾓度范围和旋转次数的变化。斯坦福大学公开课
该脚本可适⽤于uinty中需要旋转指⽰的对象,如按钮,把⼿,门等物体。早上好的英语