Adding trick spawn and movement to Experiment scene
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Spawner : MonoBehaviour
|
||||
@@ -7,6 +8,9 @@ public class Spawner : MonoBehaviour
|
||||
[SerializeField] private GameObject objectToSpawn;
|
||||
// Time interval between spawns
|
||||
public float spawnInterval = 1f;
|
||||
[SerializeField] private int maxAlive = 0;
|
||||
|
||||
private readonly List<GameObject> spawned = new List<GameObject>();
|
||||
|
||||
void Start()
|
||||
{
|
||||
@@ -21,8 +25,32 @@ public class Spawner : MonoBehaviour
|
||||
{
|
||||
// Wait for the specified interval
|
||||
yield return new WaitForSeconds(spawnInterval);
|
||||
if (objectToSpawn == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (maxAlive > 0 && CountAlive() >= maxAlive)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Spawn the object at the spawner's position and rotation
|
||||
Instantiate(objectToSpawn, transform.position, Quaternion.identity);
|
||||
var instance = Instantiate(objectToSpawn, transform.position, Quaternion.identity);
|
||||
spawned.Add(instance);
|
||||
}
|
||||
}
|
||||
|
||||
private int CountAlive()
|
||||
{
|
||||
for (var i = spawned.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (spawned[i] == null)
|
||||
{
|
||||
spawned.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
return spawned.Count;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,125 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class TrickController : MonoBehaviour
|
||||
{
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
[SerializeField] private string treatTag = "Interactable";
|
||||
[SerializeField] private float moveSpeed = 3.5f;
|
||||
[SerializeField] private float targetRefreshInterval = 0.4f;
|
||||
[SerializeField] private float consumeDistance = 0.35f;
|
||||
[SerializeField] private float wanderInterval = 1.2f;
|
||||
|
||||
private Rigidbody2D body;
|
||||
private Transform target;
|
||||
private float nextTargetTime;
|
||||
private Vector2 wanderDirection;
|
||||
private float nextWanderTime;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
|
||||
body = GetComponent<Rigidbody2D>();
|
||||
body.bodyType = RigidbodyType2D.Dynamic;
|
||||
body.gravityScale = 1f;
|
||||
body.freezeRotation = true;
|
||||
body.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
|
||||
body.interpolation = RigidbodyInterpolation2D.Interpolate;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
private void Update()
|
||||
{
|
||||
|
||||
if (Time.time >= nextTargetTime)
|
||||
{
|
||||
target = FindNearestTreat();
|
||||
nextTargetTime = Time.time + targetRefreshInterval;
|
||||
}
|
||||
|
||||
if (target == null && Time.time >= nextWanderTime)
|
||||
{
|
||||
wanderDirection = Random.insideUnitCircle.normalized;
|
||||
if (wanderDirection.sqrMagnitude < 0.01f)
|
||||
{
|
||||
wanderDirection = Vector2.right;
|
||||
}
|
||||
nextWanderTime = Time.time + wanderInterval;
|
||||
}
|
||||
|
||||
TryConsumeTreat();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
Vector2 desiredVelocity;
|
||||
var stopForTreat = false;
|
||||
if (target != null)
|
||||
{
|
||||
var direction = ((Vector2)target.position - (Vector2)transform.position).normalized;
|
||||
desiredVelocity = new Vector2(direction.x * moveSpeed, 0f);
|
||||
stopForTreat = Vector2.Distance(transform.position, target.position) <= consumeDistance;
|
||||
}
|
||||
else
|
||||
{
|
||||
desiredVelocity = new Vector2(wanderDirection.x * (moveSpeed * 0.6f), 0f);
|
||||
}
|
||||
|
||||
var velocity = body.linearVelocity;
|
||||
velocity.x = stopForTreat ? 0f : desiredVelocity.x;
|
||||
body.linearVelocity = velocity;
|
||||
}
|
||||
|
||||
private Transform FindNearestTreat()
|
||||
{
|
||||
var treats = GameObject.FindGameObjectsWithTag(treatTag);
|
||||
if (treats == null || treats.Length == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Transform best = null;
|
||||
var bestDistance = float.MaxValue;
|
||||
var origin = transform.position;
|
||||
for (var i = 0; i < treats.Length; i++)
|
||||
{
|
||||
var candidate = treats[i];
|
||||
if (!candidate.activeInHierarchy)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!candidate.TryGetComponent<Collider2D>(out var collider) || !collider.enabled)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var distance = Vector2.Distance(origin, candidate.transform.position);
|
||||
if (distance < bestDistance)
|
||||
{
|
||||
bestDistance = distance;
|
||||
best = candidate.transform;
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
private void TryConsumeTreat()
|
||||
{
|
||||
var hits = Physics2D.OverlapCircleAll(transform.position, consumeDistance);
|
||||
for (var i = 0; i < hits.Length; i++)
|
||||
{
|
||||
var hit = hits[i];
|
||||
if (hit == null || !hit.CompareTag(treatTag))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Destroy(hit.gameObject);
|
||||
if (target == hit.transform)
|
||||
{
|
||||
target = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 246e7020310b45d4d92f3af76f126c12
|
||||
guid: a8cb5c6bd0dd4b8281aec250714b6fc1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
Reference in New Issue
Block a user