四元数 LookRotation
Quaternion LookRotation
我正在尝试在塔防风格游戏的 c# 脚本中遵循一些 unity3d 示例。我需要一个炮塔 'aim' 在另一个游戏对象上。我发现的例子似乎没有说明不在 0,0,0 的原点。意思是,当炮塔在不同的位置时,它的目标是基于一个起点,而不是它的当前位置。
它现在的表现如何:
http://screencast.com/t/Vx35LJXRKNUm
在我使用的脚本中,如何提供有关炮塔当前位置的 Quaternion.LookRotation 信息以将其包含在计算中?脚本,函数 CalculateAimPosition,第 59 行:
using UnityEngine;
using System.Collections;
public class TurretBehavior : MonoBehaviour {
public GameObject projectile;
public GameObject muzzleEffect;
public float reloadTime = 1f;
public float turnSpeed = 5f;
public float firePauseTime = .25f;
public Transform target;
public Transform[] muzzlePositions;
public Transform turretBall;
private float nextFireTime;
private float nextMoveTime;
private Quaternion desiredRotation;
private Vector3 aimPoint;
// Update is called once per frame
void Update ()
{
if (target)
{
if (Time.time >= nextMoveTime)
{
CalculateAimPosition(target.position);
transform.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * turnSpeed);
}
if (Time.time >= nextFireTime) {
FireProjectile();
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "TurretEnemy")
{
nextFireTime = Time.time +(reloadTime *.5f);
target = other.gameObject.transform;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.transform == target) {
target = null;
}
}
void CalculateAimPosition(Vector3 targetPosition)
{
aimPoint = new Vector3 (targetPosition.x, targetPosition.y, targetPosition.z);
desiredRotation = Quaternion.LookRotation (aimPoint);
}
void FireProjectile()
{
nextFireTime = Time.time + reloadTime;
nextMoveTime = Time.time + firePauseTime;
foreach(Transform transform in muzzlePositions)
{
Instantiate(projectile, transform.position, transform.rotation);
}
}
}
错误在 Quaternion.LookRotation 的用法中。
该函数采用两个 Vector3
作为输入,它们是世界 space 中的前向(和一个可选的向上向量 - 默认值 Vector3.up
),以及 returns Quaternion
表示这样一个参考系的方向。
您反而提供了一个世界 space 位置作为输入 (targetPosition
),这是没有意义的。意外地,在世界中表达的归一化位置向量 space 是从原点到给定点的方向,因此当塔放在原点上时它可以正常工作。
您需要用作 LookRotation 参数的是世界 space 从塔到目标的方向:
Vector3 aimDir = (targetPosition - transform.position).normalized;
desiredRotation = Quaternion.LookRotation (aimDir );
我正在尝试在塔防风格游戏的 c# 脚本中遵循一些 unity3d 示例。我需要一个炮塔 'aim' 在另一个游戏对象上。我发现的例子似乎没有说明不在 0,0,0 的原点。意思是,当炮塔在不同的位置时,它的目标是基于一个起点,而不是它的当前位置。
它现在的表现如何: http://screencast.com/t/Vx35LJXRKNUm
在我使用的脚本中,如何提供有关炮塔当前位置的 Quaternion.LookRotation 信息以将其包含在计算中?脚本,函数 CalculateAimPosition,第 59 行:
using UnityEngine;
using System.Collections;
public class TurretBehavior : MonoBehaviour {
public GameObject projectile;
public GameObject muzzleEffect;
public float reloadTime = 1f;
public float turnSpeed = 5f;
public float firePauseTime = .25f;
public Transform target;
public Transform[] muzzlePositions;
public Transform turretBall;
private float nextFireTime;
private float nextMoveTime;
private Quaternion desiredRotation;
private Vector3 aimPoint;
// Update is called once per frame
void Update ()
{
if (target)
{
if (Time.time >= nextMoveTime)
{
CalculateAimPosition(target.position);
transform.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * turnSpeed);
}
if (Time.time >= nextFireTime) {
FireProjectile();
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "TurretEnemy")
{
nextFireTime = Time.time +(reloadTime *.5f);
target = other.gameObject.transform;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.transform == target) {
target = null;
}
}
void CalculateAimPosition(Vector3 targetPosition)
{
aimPoint = new Vector3 (targetPosition.x, targetPosition.y, targetPosition.z);
desiredRotation = Quaternion.LookRotation (aimPoint);
}
void FireProjectile()
{
nextFireTime = Time.time + reloadTime;
nextMoveTime = Time.time + firePauseTime;
foreach(Transform transform in muzzlePositions)
{
Instantiate(projectile, transform.position, transform.rotation);
}
}
}
错误在 Quaternion.LookRotation 的用法中。
该函数采用两个 Vector3
作为输入,它们是世界 space 中的前向(和一个可选的向上向量 - 默认值 Vector3.up
),以及 returns Quaternion
表示这样一个参考系的方向。
您反而提供了一个世界 space 位置作为输入 (targetPosition
),这是没有意义的。意外地,在世界中表达的归一化位置向量 space 是从原点到给定点的方向,因此当塔放在原点上时它可以正常工作。
您需要用作 LookRotation 参数的是世界 space 从塔到目标的方向:
Vector3 aimDir = (targetPosition - transform.position).normalized;
desiredRotation = Quaternion.LookRotation (aimDir );