고스트가 좌우로 반전 이동(Flip Moving)하는 걸 버튼으로 일시정지 / 재시작 제어하는 방법. Pause버튼을 클릭하면 이동하는 고스트가 멈추고 버튼의 텍스트는 Start로 변경됩니다. Start를 누르면 다시 고스트가 이동.
[PauseControl.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PauseControl : MonoBehaviour
{
[SerializeField] Text startPauseText;
bool pauseActive = false;
public void pauseBtn()
{
if (pauseActive)
{
Time.timeScale = 1;
pauseActive = false;
}
else
{
Time.timeScale = 0;
pauseActive = true;
}
startPauseText.text = pauseActive ? "START" : "PAUSE";
}
}
[GhostMove.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GhostMove : MonoBehaviour
{
Rigidbody2D rb;
Vector3 localScale;
[SerializeField] float speed = 5, dist = 7;
bool dirRight = true;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
localScale = transform.localScale;
}
void FixedUpdate()
{
if (transform.position.x > dist)
dirRight = false;
else if (transform.position.x < -dist)
dirRight = true;
if (dirRight)
GoRight();
else
GoLeft();
}
void GoRight()
{
localScale.x = 1f;
transform.transform.localScale = localScale;
rb.velocity = new Vector2(speed, 0);
}
void GoLeft()
{
localScale.x = -1f;
transform.transform.localScale = localScale;
rb.velocity = new Vector2(-speed, 0);
}
}
댓글