[英]Why the cannon is never rotating facing at the target?
我想要做的是使大炮/炮塔自動朝目標旋轉。 但是,當我運行游戲並在場景視圖窗口中移動目標時,加農炮朝向另一個方向而不是目標。
要旋轉的腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateTurret : MonoBehaviour
{
[SerializeField]
private float turnRateRadians = 2 * Mathf.PI;
[SerializeField]
private Transform turretTop; // the gun part that rotates
[SerializeField]
private Transform bulletSpawnPoint;
[SerializeField]
public GameObject target;
void Update()
{
TargetEnemy();
}
void TargetEnemy()
{
if (target != null)
{
Vector3 targetDir = target.transform.position - transform.position;
// Rotating in 2D Plane...
targetDir.y = 0.0f;
targetDir = targetDir.normalized;
Vector3 currentDir = turretTop.forward;
currentDir = Vector3.RotateTowards(currentDir, targetDir, turnRateRadians * Time.deltaTime, 1.0f);
Quaternion qDir = new Quaternion();
qDir.SetLookRotation(currentDir, Vector3.up);
turretTop.rotation = qDir;
}
}
}
這是一個截圖,顯示了腳本的附加位置,當我與他們一起移動時,在場景視圖中,目標立方體在大炮周圍尚未來回地前進,大炮始終朝向另一個方向:
這是在游戲運行后立即朝另一方向自動運行炮塔,並且在移動目標時,炮塔立方體將繼續朝另一個方向旋轉:
我認為turretTop.forward代表模型的局部坐標的正Z方向,但是您的桶被指定為模型的X軸。 也許如果您更換:
Vector3 currentDir = turretTop.forward;
有:
Vector3 currentDir = turretTop.right;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.