Загрузка страницы

Using Angle Calculations to Move Turrets

This video demonstrates how aim a turret positioned on any angled surface to face an object. This video shows code that rotates the turret, then tilts the barrel to fire a laser at a moving object. Code examples below if you would like to try:

using UnityEngine;

public class TurretAuto : MonoBehaviour
{
public fireProjectile weapon;
public Transform target;
public Transform barrel;

private void Update()
{
Aim();
weapon.fire();
}

private void Aim()
{
// TURN
float targetPlaneAngle = vector3AngleOnPlane(target.position, transform.position, -transform.up, transform.forward);
Vector3 newRotation = new Vector3(0, targetPlaneAngle, 0);
transform.Rotate(newRotation, Space.Self);

// UP/DOWN
float upAngle = Vector3.Angle(target.position, barrel.transform.up);
Vector3 upRotation = new Vector3(-upAngle + 90, 0, 0);
barrel.transform.Rotate(upRotation, Space.Self);
}

float vector3AngleOnPlane(Vector3 from, Vector3 to, Vector3 planeNormal, Vector3 toZeroAngle)
{
Vector3 projectedVector = Vector3.ProjectOnPlane(from - to, planeNormal);
float projectedVectorAngle = Vector3.SignedAngle(projectedVector, toZeroAngle, planeNormal);

return projectedVectorAngle;
}
}

Видео Using Angle Calculations to Move Turrets канала Bit Galaxis
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
21 сентября 2019 г. 7:08:52
00:13:32
Яндекс.Метрика