Intro to Shooter Mechanics

確認済のバージョン: 5.3

-

難易度: 初級

In this live training we'll look at some common game mechanics in shooting games including raycasting, physics projectiles and a simple health script.

Intro to Shooter Mechanics

初級 Scripting

RayCastShootScript

Code snippet

using UnityEngine;
using System.Collections;

public class RayCastShootScript : MonoBehaviour {

    public float fireRate = .25f;
    public float range = 50;
    public ParticleSystem smokeParticles;
    public GameObject hitParticles;
    public GameObject shootFlare;
    public int damage = 1;
    public Transform gunEnd;

    private Camera fpsCam;
    private LineRenderer lineRenderer;
    private WaitForSeconds shotLength = new WaitForSeconds(.07f);
    private AudioSource source;
    private float nextFireTime;

    void Awake()
    {
        lineRenderer = GetComponent<LineRenderer> ();
        source = GetComponent<AudioSource> ();
        fpsCam = GetComponentInParent<Camera> ();
    }


    // Update is called once per frame
    void Update () 
    {
        RaycastHit hit;
        Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3 (.5f, .5f, 0));

        if (Input.GetButtonDown("Fire1") && Time.time > nextFireTime)
        {
            nextFireTime = Time.time + fireRate;

            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, range))
            {
                IDamageable dmgScript = hit.collider.gameObject.GetComponent<IDamageable>();
                if (dmgScript != null)
                {
                    dmgScript.Damage(damage, hit.point);
                }

                if(hit.rigidbody != null)
                {
                    hit.rigidbody.AddForce(-hit.normal * 100f);
                }

                lineRenderer.SetPosition(0, gunEnd.position);
                lineRenderer.SetPosition(1, hit.point);
                Instantiate(hitParticles, hit.point, Quaternion.identity);
            }
            StartCoroutine (ShotEffect ());
        }

    }


    private IEnumerator ShotEffect()
    {
        lineRenderer.enabled = true;
        source.Play ();
        smokeParticles.Play ();
        shootFlare.SetActive (true);
        yield return shotLength;
        lineRenderer.enabled = false;
        shootFlare.SetActive (false);
    }
}

ProjectileShootScript

Code snippet

using UnityEngine;
using System.Collections;

public class ProjectileShootScript : MonoBehaviour {

    public Rigidbody projectile;
    public Transform bulletSpawn;
    public float projectileForce = 500f;
    public float fireRate = .25f;

    private float nextFireTime;

    
    // Update is called once per frame
    void Update () 
    {
        if (Input.GetButtonDown ("Fire2") && Time.time > nextFireTime) 
        {
            Rigidbody cloneRb = Instantiate (projectile, bulletSpawn.position, Quaternion.identity) as Rigidbody;
            cloneRb.AddForce(bulletSpawn.transform.forward * projectileForce);
            nextFireTime = Time.time + fireRate;
        }
    }
}

IDamageable

Code snippet

using UnityEngine;

public interface IDamageable 
{

    void Damage(int damage, Vector3 hitPoint);
}

Explode

Code snippet

using UnityEngine;
using System.Collections;

public class Explode : MonoBehaviour {

    public GameObject explosionParticles;
    public float blastRadius = 1;
    public int damage = 1;

    private bool explode;

    void OnCollisionEnter()
    {

        explosionParticles.SetActive (true);
        explosionParticles.transform.SetParent (null);
        explode = true;


    }

    void FixedUpdate()
    {
        if (explode) {
            Collider[] hitColliders = Physics.OverlapSphere (transform.position, blastRadius);
            for (int i = 0; i < hitColliders.Length; i++) 
            {
                if (hitColliders [i].GetComponent<IDamageable> () != null) 
                {
                    hitColliders [i].GetComponent<IDamageable> ().Damage (damage, hitColliders[i].transform.position);
                }

                if (hitColliders [i].GetComponent<Rigidbody> () != null)
                {
                    hitColliders [i].GetComponent<Rigidbody> ().AddExplosionForce (6000, transform.position, blastRadius);
                }
            }
            this.gameObject.SetActive (false);
        } 
    }
    
}

EnemyHealth

Code snippet

using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour, IDamageable {

    public int startingHealth = 3;
    public GameObject hitParticles;

    private int currentHealth;

    void Start()
    {
        currentHealth = startingHealth;
    }

    public void Damage(int damage, Vector3 hitPoint)
    {
        Instantiate(hitParticles, hitPoint, Quaternion.identity);
        currentHealth -= damage;
        if (currentHealth <= 0) 
        {
            Defeated();
        }
    }

    void Defeated()
    {
        gameObject.SetActive (false);
    }
    
}

関連するチュートリアル