ゾンビFPS_銃声の実装

+ボタン押下でコードを確認できます!

 

コピペしても正しく動かない場合は
エディタでアタッチし忘れている可能性が高いです。

FPSController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FPSController : MonoBehaviour
{
float x, z;
float speed = 0.1f;
public GameObject cam;
Quaternion cameraRot, characterRot;
float Xsensityvity = 3f, Ysensityvity = 3f;
bool cursorLock = true;
float minX = -90f, maxX = 90f;
public Animator animator;
int ammunition = 50, maxAmmunition = 50, ammoClip = 10, maxAmmoClip = 10;
int playerHP = 100, maxPlayerHP = 100;
public Slider hpBer;
public Text ammoText;
public GameObject meinCamera, subCamera;
public AudioSource playerFootStep;
public AudioClip WalkFootStepSE, RunFootStepSE;
// Start is called before the first frame update
void Start()
{
cameraRot = cam.transform.localRotation;
characterRot = transform.localRotation;
GameState.canShoot = true;
hpBer.value = playerHP;
ammoText.text = ammoClip + "/" + ammunition;
}
// Update is called once per frame
void Update()
{
float xRot = Input.GetAxis("Mouse X") * Ysensityvity;
float yRot = Input.GetAxis("Mouse Y") * Xsensityvity;
cameraRot *= Quaternion.Euler(-yRot, 0, 0);
characterRot *= Quaternion.Euler(0, xRot, 0);
cameraRot = ClampRotation(cameraRot);
cam.transform.localRotation = cameraRot;
transform.localRotation = characterRot;
UpdateCursorLock();
if (Input.GetMouseButton(0) && GameState.canShoot)
{
if (ammoClip > 0)
{
animator.SetTrigger("Fire");
GameState.canShoot = false;
ammoClip--;
ammoText.text = ammoClip + "/" + ammunition;
}
else
{
//Debug.Log("弾がないよ");
Weapon.instance.TriggerSE();
}
}
if (Input.GetKeyDown(KeyCode.R))
{
int amountNeed = maxAmmoClip - ammoClip;
int ammoAvailable = amountNeed < ammunition ? amountNeed : ammunition;
if (amountNeed != 0 && ammunition != 0)
{
animator.SetTrigger("Reload");
ammunition -= ammoAvailable;
ammoClip += ammoAvailable;
ammoText.text = ammoClip + "/" + ammunition;
}
}
if (Mathf.Abs(x) > 0 || Mathf.Abs(z) > 0)
{
if (!animator.GetBool("Walk"))
{
animator.SetBool("Walk", true);
PlayerWalkFootStep(WalkFootStepSE);
}
}
else if (animator.GetBool("Walk"))
{
animator.SetBool("Walk", false);
StopFootStep();
}
if (z > 0 && Input.GetKey(KeyCode.LeftShift))
{
if (!animator.GetBool("Run"))
{
animator.SetBool("Run", true);
speed = 0.25f;
PlayerRunFootStep(RunFootStepSE);
}
}
else if (animator.GetBool("Run"))
{
animator.SetBool("Run", false);
speed = 0.1f;
StopFootStep();
}
if (Input.GetMouseButton(1))
{
subCamera.SetActive(true);
meinCamera.GetComponent<Camera>().enabled = false;
}
else if (subCamera.activeSelf)
{
subCamera.SetActive(false);
meinCamera.GetComponent<Camera>().enabled = true;
}
}
private void FixedUpdate()
{
x = 0;
z = 0;
x = Input.GetAxisRaw("Horizontal") * speed;
z = Input.GetAxisRaw("Vertical") * speed;
//transform.position += new Vector3(x, 0, z);
transform.position += cam.transform.forward * z + cam.transform.right * x;
}
public void UpdateCursorLock()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
cursorLock = false;
}
else if (Input.GetMouseButton(0))
{
cursorLock = true;
}
if (cursorLock)
{
Cursor.lockState = CursorLockMode.Locked;
}
else if (!cursorLock)
{
Cursor.lockState = CursorLockMode.None;
}
}
public Quaternion ClampRotation(Quaternion q)
{
//q = x,y,z,w (x,y,zはベクトル(量と向き):wはスカラー(座標とは無関係の量))
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1f;
float angleX = Mathf.Atan(q.x) * Mathf.Rad2Deg * 2f;
angleX = Mathf.Clamp(angleX, minX, maxX);
q.x = Mathf.Tan(angleX * Mathf.Deg2Rad * 0.5f);
return q;
}
public void PlayerWalkFootStep(AudioClip clip)
{
playerFootStep.loop = true;
playerFootStep.pitch = 1f;
playerFootStep.clip = clip;
playerFootStep.Play();
}
public void PlayerRunFootStep(AudioClip clip)
{
playerFootStep.loop = true;
playerFootStep.pitch = 1.3f;
playerFootStep.clip = clip;
playerFootStep.Play();
}
public void StopFootStep()
{
playerFootStep.Stop();
playerFootStep.loop = false;
playerFootStep.pitch = 1f;
}
}
Weapon
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public AudioSource weapon;
public AudioClip relodingSE, fireSE, triggerSE;
public static Weapon instance;
private void Awake()
{
if (instance == null)
{
instance = this;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void CanShoot()
{
GameState.canShoot = true;
}
public void FireSE()
{
weapon.clip = fireSE;
weapon.Play();
}
public void RelodingSE()
{
weapon.clip = relodingSE;
weapon.Play();
}
public void TriggerSE()
{
if (!weapon.isPlaying)
{
weapon.clip = triggerSE;
weapon.Play();
}
}
}
view raw Weapon.cs hosted with ❤ by GitHub

 

他の講座クーポン【93%OFF】

関連記事

クーポン期限2023/01/24 Udemyでは定価24000円になっていますがこのクーポンページからの購入で1800円台で購入することが可能になっています! 30日以内な[…]

unity fps

 

Udemyよりお得に講座視聴可能なYouTubeメンバーシップ