나는 타워 방어 스타일 게임을위한 C# 스크립팅에서 몇 가지 unity3d exmaples을 따르려고합니다. 다른 게임 개체를 목표로 포탑이 필요합니다. 내가 찾은 예제는 0,0,0에없는 원점을 설명하지 않는 것 같습니다. 의미, 터렛이 다른 위치에있을 때, 현재 위치가 아닌 시작점을 기준으로합니다.Quaternion LookRotation
어떻게 지금 작동하고 : 내가 사용하고있는 스크립트에서 http://screencast.com/t/Vx35LJXRKNUm
, 어떻게 그것을 계산의에 포함하기 위해 포탑의 현재 위치에 대한 Quaternion.LookRotation 정보를 제공합니까? 스크립트 기능 CalculateAimPosition, line59 :
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);
}
}
}
감사합니다가. 이는 매우 분명한 답변입니다. 세부 사항에 대한 관심에 대해 이야기하고 코드를 편집 할 때 줄 번호에 대한 참조를 편집했습니다. 감사 – Roger