Shark Feeding Game(상어먹방게임) - 안드로이드 폰 화면을 터치하면 물고기(Prefabs)가 떨어지고(Touch & Spawn) 좌우반전 이동(Object Flip Move)하는 상어 입속에 들어가면 효과음(Collision Sfx)이 발생하면서 물고기가 사라지게 됩니다(OnTriggerEnter2D - Destroy).
[SharkMove.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SharkMove : MonoBehaviour
{
Rigidbody2D rb;
Vector3 localScale;
[SerializeField] float speed = 5, dist = 7;
bool dirRight = true;
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 = -1;
transform.transform.localScale = localScale;
rb.velocity = new Vector2(speed, 0);
}
void GoLeft()
{
localScale.x = 1;
transform.transform.localScale = localScale;
rb.velocity = new Vector2(-speed, 0);
}
}
[DestroyEat.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyEat : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D collision)
{
FishSpawn.SoundPlay();
Destroy(collision.gameObject);
}
}
[DestroyArea.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyArea : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D collision)
{
Destroy(collision.gameObject);
}
}
[FishSpawn.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FishSpawn : MonoBehaviour
{
[SerializeField] GameObject fish;
static AudioSource audioSource;
public static AudioClip audioClip;
void Start()
{
audioSource = GetComponent<AudioSource>(); audioClip = Resources.Load<AudioClip>("Eat");
}
void Update()
{
if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
if (touch.phase == TouchPhase.Began)
Instantiate(fish, touchPos, Quaternion.identity);
}
}
public static void SoundPlay()
{
audioSource.PlayOneShot(audioClip);
}
}
댓글