Merry Fragmas: Multiplayer FPS, Part 1

確認済のバージョン: 4.6

-

難易度: 中級

Merry Fragmas everyone! During this live training series, we will be building a multiplayer First Person Shooter game. We will be focusing on the animation, physics, networking (with Photon), and other tricks that make FPS games what they are. Content will range from beginner to intermediate+ (depending on what you consider difficult). We will be looking at a lot of scripting and a few pro features (though you should still be able to complete the training without pro). I have no idea how many days this will span, so I am scheduling it for 3. Please note that I do not know how many days will be required to complete this series and what is scheduled is just a guess. With that being said, let us crush our foes with holiday spirit! -Tutor: Mike Geig

Merry Fragmas: Multiplayer FPS, Part 1

中級 The Asset Store

PlayerShooting

Code snippet

using UnityEngine;
using System.Collections;

public class PlayerShooting : MonoBehaviour {

    public ParticleSystem muzzleFlash;
    Animator anim;
    public GameObject impactPrefab;

    GameObject[] impacts;
    int currentImpact = 0;
    int maxImpacts = 5;

    bool shooting = false;

    // Use this for initialization
    void Start () {
    
        impacts = new GameObject[maxImpacts];
        for(int i = 0; i < maxImpacts; i++)
            impacts[i] = (GameObject)Instantiate(impactPrefab);

        anim = GetComponentInChildren ();
    }
    
    // Update is called once per frame
    void Update () {
    
        if(Input.GetButtonDown ("Fire1") && !Input.GetKey(KeyCode.LeftShift))
        {
            muzzleFlash.Play();
            anim.SetTrigger("Fire");
            shooting = true;
        }

    }

    void FixedUpdate()
    {
        if(shooting)
        {
            shooting = false;

            RaycastHit hit;

            if(Physics.Raycast(transform.position, transform.forward, out hit, 50f))
            {
                if(hit.transform.tag == "Enemy")
                    Destroy (hit.transform.gameObject);

                impacts[currentImpact].transform.position = hit.point;
                impacts[currentImpact].GetComponent().Play();

                if(++currentImpact >= maxImpacts)
                    currentImpact = 0;
            }
        }
    }
}

関連するチュートリアル