Adding trick spawn and movement to Experiment scene
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user