Pacman/Assets/Scripts/Ghost_Home.cs
2025-04-20 21:10:16 +05:30

70 lines
1.8 KiB
C#

using System.Collections;
using UnityEngine;
public class Ghost_Home : Ghost_behaviour
{
public Transform inside;
public Transform outside;
private void OnEnable()
{
StopAllCoroutines();
}
private void OnDisable()
{
if(this.gameObject.activeSelf)
{
StartCoroutine(ExitTransition());
}
}
private void OnCollisionEnter2D(Collision2D other)
{
if(this.enabled && other.gameObject.layer == LayerMask.NameToLayer("Obstacke"))
{
this.ghost.movement.SetDirection(-this.ghost.movement.direction);
}
}
private IEnumerator ExitTransition()
{
this.ghost.movement.SetDirection(Vector2.up, true);
this.ghost.movement.rigidbody.isKinematic =true;
this.ghost.movement.enabled = false;
Vector3 position = this.transform.position;
float duration = 0.5f;
float elapsed = 0.0f;
while( elapsed < duration)
{
Vector3 newPosition = Vector3.Lerp(position, this.inside.position, elapsed / duration);
newPosition.z =position.z;
transform.position = newPosition;
elapsed += Time.deltaTime;
yield return null;
}
elapsed = 0.0f;
while( elapsed < duration)
{
Vector3 newPosition = Vector3.Lerp(this.inside.position,this.outside.position, elapsed / duration);
newPosition.z =position.z;
transform.position = newPosition;
elapsed += Time.deltaTime;
yield return null;
}
this.ghost.movement.SetDirection(new Vector2(Random.value < 0.5f ? -1.0f : 1.0f, 0), true);
this.ghost.movement.rigidbody.isKinematic =false;
this.ghost.movement.enabled = true;
}
}