2017-11-24 8 views
0

これはスクリプトでもあり、スポットライトにアタッチして、プレイヤーがウェイポイント間を移動しているときにスポットライトを回転させて点灯させます。特定の範囲のプレーヤーを検出すると、タレットが回転しないのはなぜですか?

しかし、私もタレットにスクリプトを添付すると、私は、砲台が180度のように回転して停止し、プレーヤーを追跡し続けることはありません。

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class RotateSpotlight : MonoBehaviour 
{ 
    public GameObject target; 
    public float smooth = 1f; 
    public float rangeSqr; 
    public float rotationSpeed; 
    Quaternion originalRotation; 

    private void Start() 
    { 
     originalRotation = transform.localRotation; 
    } 

    private void Update() 
    { 
     if (target.transform.position.x < transform.position.x + rangeSqr) 
     { 
      var targetRotation = Quaternion.LookRotation(target.transform.position - transform.position); 
      var str = Mathf.Min(rotationSpeed * Time.deltaTime, 1); 
      transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str); 
     } 
     else 
     { 
      var str = Mathf.Min(rotationSpeed * Time.deltaTime, 1); 
      transform.rotation = Quaternion.Lerp(transform.rotation, originalRotation, str); 
     } 
    } 
} 

Turret

+0

に見てみることができますか? – roelofs

+0

@roelofsなぜタレットはプレーヤーに向かって回転し続けていないのですか?プレーヤーはウェイポイント間を移動していますが、タレットはそれを追跡していません。しかし、例えばスポットライトにスクリプトが添付されている場合、スポットライトは、プレーヤーが範囲内にいるときにプレーヤーを追跡し続けるでしょう。 –

+0

タレットコードはどのように見えますか?私はこれがあまりにも情報が少ないと思う。 – roelofs

答えて

0

それがより良いとPOS xとPOS yを比較するよりも、VectorX.Distance()を使用して、より応答です。

私はあなたの問題がQuaternion.LookRotationから来ていると確信していますが、この関数はあなたが期待した値をあなたに与えません。

個人的に私は、角度計算のためにこれを使用します。

Vector3 a = targetPos - _transform.position; 
    Vector3 b = Vector3.forward; 
    float angle = Vector3.Angle(a, b); 
    float finalAngle = (Vector3.Cross(a, b).y > 0 ? -angle : angle); 


あなたは正確にあなたの質問は何ですか「のcomplet」コード

private void Update() 
{ 
    //check range 
    if (Vector3.Distance(targetPos, turretPos) < maxRange) 
     AutoAttack(targetPos); 
} 
public override void AutoAttack(Vector3 targetPos) 
{ 
    Vector3 a = targetPos - _transform.position; 
    Vector3 b = Vector3.forward; 
    float angle = Vector3.Angle(a, b); 
    Rotation(Vector3.Cross(a, b).y > 0 ? -angle : angle); 

    //this condition is to to fire only when the turret is currently looking at target 
    if (Mathf.Abs(Mathf.DeltaAngle(_transform.eulerAngles.y, (Vector3.Cross(a, b).y > 0 ? -angle : angle))) < minAngleToFire) 
     Attacking(); 
} 
//call to rotate the in y axis 
public void RotationY(float angle) 
{ 
    _transform.rotation = Quaternion.Lerp(_transform.rotation, Quaternion.Euler(0, angle, 0), Time.deltaTime * rotationSpeed); 
} 
関連する問題