Creating a Casual Jewel Mining Game

確認済のバージョン: 4.6

-

難易度: 初級

In this live training session we look at creating a casual jewel mining game where the player clicks to fire a hook and drag in jewels or rocks before a timer runs out.

Creating a Casual Jewel Mining Game

初級 課題に挑戦

Code snippet

using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour {

    public GameObject claw;
    public bool isShooting;
    public Animator minerAnimator;
    public Claw clawScript;
    

    void Update () 
    {
        if (Input.GetButtonDown("Fire1") && !isShooting)
        {
            LaunchClaw();
        }

    }

    void LaunchClaw()
    {
        isShooting = true;
        minerAnimator.speed = 0;
        RaycastHit hit;
        Vector3 down = transform.TransformDirection(Vector3.down);

        if(Physics.Raycast(transform.position, down, out hit, 100))
        {
            claw.SetActive(true);
            clawScript.ClawTarget(hit.point);
        }
    }

    public void CollectedObject()
    {
        isShooting = false;
        minerAnimator.speed = 1;
    }
}

Claw

Code snippet

using UnityEngine;
using System.Collections;

public class Claw : MonoBehaviour {

    public Transform origin;
    public float speed = 4f;
    public Gun gun;
    public ScoreManager scoreManager;

    private Vector3 target;
    private int jewelValue = 100;
    private GameObject childObject;
    private LineRenderer lineRenderer;
    private bool hitJewel;
    private bool retracting;

    
    void Awake ()
    {
        lineRenderer = GetComponent<LineRenderer>();
    }


    void Update () 
    {
        float step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards (transform.position, target, step);
        lineRenderer.SetPosition(0, origin.position);
        lineRenderer.SetPosition(1, transform.position);
        if (transform.position == origin.position && retracting)
        {
            gun.CollectedObject();
            if (hitJewel)
            {
                scoreManager.AddPoints(jewelValue);
                hitJewel = false;
            }
            Destroy(childObject);
            gameObject.SetActive(false);
        }
    }

    public void ClawTarget (Vector3 pos)
    {
        target = pos;
    }

    void OnTriggerEnter (Collider other)
    {
        retracting = true;
        target = origin.position;

        if (other.gameObject.CompareTag("Jewel"))
        {
            hitJewel = true;
            childObject = other.gameObject;
            other.transform.SetParent(this.transform);
        }

        else if (other.gameObject.CompareTag("Rock"))
        {
            childObject = other.gameObject;
            other.transform.SetParent(this.transform);
        }
    }
}

ScoreManager

Code snippet

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour {

    public int score = 0;
    public int targetScore = 400;
    public Text scoreText;
    public Text timeText;
    public int timePerLevel = 30;
    public GameObject youWon;
    public GameObject gameOver;


    private float clockSpeed = 1f;

    
    void Awake () 
    {
        scoreText.text = ("Score: " + score + "/" + targetScore);

        InvokeRepeating("Clock", 0, clockSpeed);
    }

    void Clock()
    {
        timePerLevel--;
        timeText.text = ("Time: " + timePerLevel);
        if (timePerLevel == 0)
        {
            CheckGameOver();
        }
    }

    public void AddPoints(int pointScored)
    {
        score += pointScored;
        scoreText.text = ("Score: " + score + "/" + targetScore);
    }

    void CheckGameOver()
    {
        if (score >= targetScore)
        {
            Time.timeScale = 0;
            youWon.SetActive(true);
        }
        else
        {
            Time.timeScale = 0;
            gameOver.SetActive(true);
        }
    }

}

SpawnJewels

Code snippet

using UnityEngine;
using System.Collections;

public class SpawnJewels : MonoBehaviour {

    public int xRange = 10;
    public int yRange = 3;
    public int numObjects = 16;

    public GameObject[] objects;

    // Use this for initialization
    void Start () 
    {
        Spawn ();
    }
    
    void Spawn()
    {
        for (int i = 0; i <= numObjects; i++)
        {
            Vector3 spawnLoc = new Vector3 (Random.Range (-xRange, xRange), Random.Range (-yRange, yRange), 0);
            int objectPick = Random.Range (0, objects.Length);
            Instantiate(objects[objectPick], spawnLoc, Random.rotation);
        }
    }
}