![](/img/trans.png)
[英]How can I change transform name at specific degrees while the transform is rotating?
[英]Why the transform is rotating 360 degrees even if I limit the rotation randomly between specific degrees ranges?
槍正在旋轉 360 度,而不是在 -40 和 0 之間
此屏幕截圖是運行游戲前的默認設置,x 上的旋轉為 0 :
這個截圖是在我將槍械 x 旋轉值更改為 -40 之后
我希望槍在 0 到 -40 之間向上旋轉,但是當我運行游戲時,槍旋轉了 360 度。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MissleLauncherRotation : MonoBehaviour
{
public Transform body;
public Transform guns;
public Vector3 spinAxisBody;
public Vector3 spinAxisGuns;
public float timeToSpin = 5f;
public float spinSpeed = 20f;
public bool randomSpin = false;
private void Start()
{
StartCoroutine("Spin");
}
private void Update()
{
}
IEnumerator Spin()
{
float spinTimer;
while (true)
{
if (randomSpin == true)
{
spinAxisBody = new Vector3(0,Random.Range(-180, 180),0);
spinAxisGuns = new Vector3(Random.Range(-40, 0), 0, 0);
}
spinTimer = timeToSpin;
while (spinTimer > 0f)
{
body.transform.Rotate(spinAxisBody, Time.deltaTime * spinSpeed);
guns.Rotate(spinAxisGuns, Time.deltaTime * spinSpeed);
spinTimer -= Time.deltaTime;
yield return null;
}
}
}
}
我試過這個:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MissleLauncherRotation : MonoBehaviour
{
public Transform body;
public Transform guns;
public Vector3 spinAxisBody;
public Vector3 spinAxisGuns;
public float timeToSpin = 5f;
public float spinSpeed = 20f;
public bool randomSpin = false;
Quaternion rot;
private void Start()
{
rot = guns.rotation;
}
private void Update()
{
guns.Rotate(guns.up, (spinSpeed * Time.deltaTime));
var rot = guns.rotation.eulerAngles;
rot = new Vector3
(
guns.eulerAngles.x,
Mathf.Clamp(rot.x, -40, 0),
guns.eulerAngles.z
);
guns.rotation = Quaternion.Euler(rot);
}
}
現在唯一起作用的是夾緊,但夾緊不僅在 x 上,我想使用協程或以某種方式使其每 x 秒旋轉一次。
我這樣做是為了測試夾緊是否正常工作,現在我如何將它與我的第一個代碼一起使用?
如前所述, Rotate
向當前Rotate
添加了一個旋轉。
我寧願預先計算目標旋轉並使用例如
public Transform body;
public Transform guns;
public Vector3 spinAxisBody;
public Vector3 spinAxisGuns;
public float timeToSpin = 5f;
public float spinSpeed = 20f;
public bool randomSpin = false;
IEnumerator Start()
{
while (true)
{
if (randomSpin == true)
{
spinAxisBody = new Vector3(0,Random.Range(-180, 180),0);
spinAxisGuns = new Vector3(Random.Range(-40, 0), 0, 0);
}
var startBodyRotation = body.localRotation;
var targetBodyRotation = startBodyRotation * Quaternion.Euler(spinAxisBody);
var startGunRotation = guns.localRotation;
var targetGunsRotation = startGunRotation * Quaternion.Euler(spinAxisGuns);
for(spinTimer = 0; spinTimer < timeToSpin; spinTimer += Time.deltaTime)
{
// Factor linear moving from 0 to 1 within timeToSpin seconds
var factor = spinTimer / timeToSpin;
// optional add ease-in and -out
//factor = Mathf.SmoothStep(0, 1, factor);
body.transform.localRotation = Quaternion.Lerp(startBodyRotation, targetBodyRotation, factor);
guns.localRotation(startGunRotation, targetGunsRotation, factor);
yield return null;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.