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
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
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
Brief UpdateAsset Forge Build - Small Spacecraft Docking StationSetting Up a Simple Laser SystemUnity3D Shader Graph IntroductionAn Easy Way for You to Make Models for Your GamesPost Processing and Scripted Light IntensityBlender Modeling Basics Part 1Make a Low-Poly Pumpkin in BlenderAsset Forge Build Space OutpostSetting Up a Small Space Scene In UnitySimple Enemy Behavior in Unity3DAsset Forge Build - SubmarineTips for Getting Past a Creative BlockProjectiles and Collisions IntroductionMaking Space Ships Go Boom Unity3DAsset Forge Build - Space StationA Modest Menu System in Unity3DAsset Forge Build Along - Another Space FighterSimple Enemy Ship Behavior Minor UpdateBrief Update and Asset Forge BuildSpaceflight Controls Revisited