lab5
This commit is contained in:
1 parent
8302032c9f
commit
1b564e47e9
16 files changed
+1990
-1
No files matched your search
@@ -0,0 +1,158 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Lab5
|
||||
{
|
||||
public class ProjectileLauncher : MonoBehaviour
|
||||
{
|
||||
public GameObject projectilePrefab;
|
||||
public Transform target;
|
||||
public float launchSpeed = 20f;
|
||||
public bool useHighTrajectory;
|
||||
|
||||
public float randomRadius = 10f;
|
||||
public Vector2 centerOffset = Vector2.zero;
|
||||
public GameObject targetCirclePrefab;
|
||||
public bool showTargetCircle = true;
|
||||
public float circleLifetime = 2f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (target == null)
|
||||
{
|
||||
CreateNewRandomTarget();
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Space))
|
||||
{
|
||||
LaunchProjectile();
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.R))
|
||||
{
|
||||
CreateNewRandomTarget();
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateNewRandomTarget()
|
||||
{
|
||||
var center = new Vector3(
|
||||
transform.position.x + centerOffset.x,
|
||||
0.1f,
|
||||
transform.position.z + centerOffset.y
|
||||
);
|
||||
|
||||
var randomCircle = Random.insideUnitCircle * randomRadius;
|
||||
|
||||
var randomPosition = new Vector3(
|
||||
center.x + randomCircle.x,
|
||||
0.1f,
|
||||
center.z + randomCircle.y
|
||||
);
|
||||
|
||||
if (target)
|
||||
{
|
||||
target.position = randomPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
var newTarget = new GameObject("RandomTarget")
|
||||
{
|
||||
transform =
|
||||
{
|
||||
position = randomPosition
|
||||
}
|
||||
};
|
||||
target = newTarget.transform;
|
||||
}
|
||||
|
||||
if (showTargetCircle && targetCirclePrefab)
|
||||
{
|
||||
SpawnTargetCircle(randomPosition);
|
||||
}
|
||||
}
|
||||
|
||||
private void SpawnTargetCircle(Vector3 position)
|
||||
{
|
||||
var circle = Instantiate(targetCirclePrefab, position, Quaternion.identity);
|
||||
var circlePos = circle.transform.position;
|
||||
circlePos.y = 0.05f;
|
||||
circle.transform.position = circlePos;
|
||||
|
||||
if (circleLifetime > 0)
|
||||
{
|
||||
Destroy(circle, circleLifetime);
|
||||
}
|
||||
}
|
||||
|
||||
private void LaunchProjectile()
|
||||
{
|
||||
if (!target) return;
|
||||
|
||||
var direction = Projectile.GetFireDirection(
|
||||
transform.position,
|
||||
target.position,
|
||||
launchSpeed,
|
||||
useHighTrajectory
|
||||
);
|
||||
|
||||
if (direction == Vector3.zero) return;
|
||||
|
||||
var projObj = Instantiate(projectilePrefab, transform.position, Quaternion.identity);
|
||||
var projectile = projObj.GetComponent<Projectile>();
|
||||
projectile.Set(transform.position, direction, launchSpeed);
|
||||
|
||||
CreateNewRandomTarget();
|
||||
}
|
||||
|
||||
public void SetTargetAtPosition(Vector3 position)
|
||||
{
|
||||
var targetPos = position;
|
||||
targetPos.y = 0.1f;
|
||||
|
||||
if (target != null)
|
||||
{
|
||||
target.position = targetPos;
|
||||
}
|
||||
else
|
||||
{
|
||||
var newTarget = new GameObject("ManualTarget")
|
||||
{
|
||||
transform =
|
||||
{
|
||||
position = targetPos
|
||||
}
|
||||
};
|
||||
target = newTarget.transform;
|
||||
}
|
||||
|
||||
if (showTargetCircle && targetCirclePrefab != null)
|
||||
{
|
||||
SpawnTargetCircle(targetPos);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = new Color(1f, 0f, 0f, 0.2f);
|
||||
var center = new Vector3(
|
||||
transform.position.x + centerOffset.x,
|
||||
0.1f,
|
||||
transform.position.z + centerOffset.y
|
||||
);
|
||||
Gizmos.DrawWireSphere(center, randomRadius);
|
||||
|
||||
if (target != null)
|
||||
{
|
||||
var reachable = Projectile.IsTargetReachable(transform.position, target.position, launchSpeed);
|
||||
Gizmos.color = reachable ? Color.green : Color.red;
|
||||
Gizmos.DrawLine(transform.position, target.position);
|
||||
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawSphere(target.position, 0.2f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user