top of page
Abstract Lines

Dino Runner

Genre : 2D , Side Scrolling , Endless

Platform : PC (Play in Browser)

Mode : Single Player

"Dino Runner" เป็นเกมแนว 2D Side-Scrolling แบบ Endless ที่ผู้เล่นจะรับบทเป็นไดโนเสาร์ที่ต้องหลบหลีกจากสิ่งกีดขวางและลูกไฟจากภูเขาไฟ คุณจะต้องใช้ความรวดเร็วในการตัดสินใจเพื่อเอาชีวิตรอด

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControls : MonoBehaviour
{
    private float horizontal;
    private float jumpForce = 12;
    private bool gameOver;
    private float moveSpeed = 5;
    private Rigidbody2D rb;
    [SerializeField] private Animator animator;
    private bool isGrounded;
    [SerializeField] private UI text;
    [SerializeField] private SoundManager sound;
    [SerializeField] private Sound Jump;
    [SerializeField] private Sound Hit;

    [SerializeField] private GameOver gOver;

    void Start()
    {
        gameOver = false;
        rb = GetComponent<Rigidbody2D>();

    }

    void Update()
    {
        gameOver = gOver._gameOver;
        if (!gameOver)
        {
            horizontal = Input.GetAxisRaw("Horizontal");
            transform.position += new Vector3(horizontal * Time.deltaTime * moveSpeed, 0);
            if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
            {
                rb.velocity = Vector2.up * jumpForce;
                isGrounded = false;
                sound.Play(Jump);
                animator.SetBool("isJump", true);
            }
        }
        else animator.SetTrigger("Die");
    }

    private void OnCollisionEnter2D(Collision2D col)
    {
            isGrounded = true;
            animator.SetBool("isJump", false);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name == "Fire")
        {
            text.HpUpdate(1);
            sound.Play(Hit);
        }
    }

}

PlayerControls

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scroll : MonoBehaviour
{

    private bool gameOver;
    [SerializeField] private GameOver _gameOver;
    private void Start()
    {
        gameOver = false;
    }
    // Update is called once per frame
    void Update()
    {
        gameOver = _gameOver._gameOver;
        if (!gameOver)
        {
            transform.position += new Vector3(-3 * Time.deltaTime, 0);
            if (transform.position.x < -23.8f)
                transform.position = new Vector3(23.8f, transform.position.y);
        }
    }
}





using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollBG : MonoBehaviour
{
    void Update()
    {
        transform.position += new Vector3(-1 * Time.deltaTime, 0);
        if (transform.position.x < -15f)
            transform.position = new Vector3(20f, transform.position.y);
    }
}

Scroll

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[CreateAssetMenu(menuName = "Sound")]
public class Sound : ScriptableObject
{
    [SerializeField] private AudioClip _clip;
    public AudioClip clip => _clip;

    [Range(0f,1f)]
    [SerializeField] private float _volume;
    public float volume => _volume;

    [SerializeField] private bool _loop;
    public bool loop => _loop;

    [HideInInspector] public AudioSource audioSource;
}

Sound

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundManager : MonoBehaviour
{
    [SerializeField] private Sound BGM;
    private void Start()
    {
        Play(BGM);
    }

    public void Play(Sound _sound)
    {
        _sound.audioSource = gameObject.AddComponent<AudioSource>();
        _sound.audioSource.clip = _sound.clip;
        _sound.audioSource.volume = _sound.volume;
        _sound.audioSource.loop = _sound.loop;
        _sound.audioSource.Play();
    }

}

Sound Manager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using Lean.Pool;

public class Volcano : MonoBehaviour
{
    [SerializeField] private GameObject target;
    [SerializeField] private Transform shootPoint;
    [SerializeField] private Rigidbody2D bulletPrefab;
    [SerializeField] private Animator animator;
    private float spawnDelay = 0.5f;
    private float waveDelay = 3f;
    private bool _gameOver = false;

    [SerializeField] private SoundManager sound;
    [SerializeField] private Sound Fire;

    [SerializeField] private GameOver gOver;
    void Start()
    {
        StartCoroutine(SpawnWave(bulletPrefab,spawnDelay,waveDelay,3));
    }
    void Update()
    {
        _gameOver = gOver._gameOver;
    }
    IEnumerator SpawnWave(Rigidbody2D go, float spawnDelay, float waveDelay, int countInWave)
    {
        while (!_gameOver)
        {
            yield return new WaitForSeconds(waveDelay);
            for (int i = 0; i < countInWave; i++)
            {
                Vector2 projectileVelocity = CalculateProjectileVelocity(shootPoint.position, target.transform.position, 1.5f);
                Rigidbody2D fire = LeanPool.Spawn(bulletPrefab, shootPoint.position, Quaternion.identity);
                fire.velocity = projectileVelocity;
                animator.SetTrigger("shoot");
                sound.Play(Fire);
                yield return new WaitForSeconds(spawnDelay);
            }
        }
    }

    Vector2 CalculateProjectileVelocity(Vector2 origin, Vector2 target, float time)
    {
        Vector2 distance2D = target - origin;
        Vector2 distance = distance2D;
        distance.y = 0;
        float distX = distance.magnitude;
        float distY = distance2D.y;

        //find Vx,Vy
        float vX = distX / time;
        float vY = distY / time + 0.5f * time * Mathf.Abs(Physics2D.gravity.y);
        Vector2 result = distance.normalized;
        result *= vX;
        result.y = vY;

        return result;
    }
}

Volcano

using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.UI;

public class UI : MonoBehaviour
{
    [SerializeField] private Text hpText;
    private int hp;
    [SerializeField] private PlayerControls playerControls;
    [SerializeField] private Volcano volcano;
    private bool gameover;
    [SerializeField] private Animator animator;
    [SerializeField] private GameOver _gOver;
    
    void Start()
    {
        gameover = false;
        hp = 3;
        UpdateText();
    }

    // Update is called once per frame
    void Update()
    {
        gameover = _gOver._gameOver;
        if (hp == 0)
        {
            GameOver();
        }
    }

    private void UpdateText()
    {
        if (!gameover)
        {
            hpText.text = "" + hp;
        }
    }

    public void HpUpdate(int damage)
    {
        hp -= damage;
        animator.SetTrigger("hit");
        UpdateText();
    }

    private void GameOver()
    {
        _gOver.SetGameOver(true);
    }
}

UI

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class GameOver : MonoBehaviour
{
    private bool gameOver;
    public bool _gameOver;
    [SerializeField] private GameObject button1, button2, over;
    
    void Start()
    {
        gameOver = false;
    }

    
    void Update()
    {
        _gameOver = gameOver;
    }

    public void SetGameOver(bool x)
    {
        gameOver = x;
        button1.gameObject.SetActive(true);
        button2.gameObject.SetActive(true);
        over.gameObject.SetActive(true);
    }
}

GameOver

Akaranan Weerayanphan

akaranan.weer@gmail.com

Tel : 0814323331

  • alt.text.label.Facebook

©2023 by Akaranan Weerayanphan. Proudly created with Wix.com

bottom of page