移動時の足音の実装
足音の実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(AudioSource))]
public class PlayerController : MonoBehaviour
{
public float walkSpeed;
public float runSpeed;
private Vector3 movement;
private CharacterController characterController;
private AudioSource audioSource;
public GameObject FPSCamera;
private Vector3 moveDir = Vector3.zero;
private float gravity = 9.8f;
public float jumpPower;
public AudioClip jumpSound;
// ★追加(足音)
private float runStepLengthen = 0.7f;
private float stepInterval = 10f;
private float stepCycle;
private float nextStep;
private float speed;
private bool isWalking;
public AudioClip stepSound;
private void Start()
{
characterController = GetComponent<CharacterController>();
audioSource = GetComponent<AudioSource>();
}
private void Update()
{
PlayerMove();
// ★追加(足音)
ProgressStepCycle(speed);
}
void PlayerMove()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
movement = new Vector3(moveH, 0, moveV);
Vector3 desiredMove = FPSCamera.transform.forward * movement.z + FPSCamera.transform.right * movement.x;
moveDir.x = desiredMove.x * 5f;
moveDir.z = desiredMove.z * 5f;
if (Input.GetKey(KeyCode.LeftShift))
{
characterController.Move(moveDir * Time.deltaTime * runSpeed);
// ★追加(足音)
isWalking = false;
speed = runSpeed;
}
else
{
characterController.Move(moveDir * Time.deltaTime * walkSpeed);
// ★追加(足音)
isWalking = true;
speed = walkSpeed;
}
if (characterController.isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space))
{
moveDir.y = jumpPower;
audioSource.clip = jumpSound;
audioSource.Play();
}
}
else
{
moveDir.y -= gravity * Time.deltaTime;
}
}
// ★追加(足音)
void PlayStepSound()
{
// ジャンプ中は足音を発生させない。
if(!characterController.isGrounded)
{
return;
}
audioSource.clip = stepSound;
audioSource.PlayOneShot(audioSource.clip);
}
// ★追加(足音)
// ステップサイクル
void ProgressStepCycle(float speed)
{
if(characterController.velocity.sqrMagnitude > 0 && (movement.x != 0 || movement.z != 0))
{
// 三項演算子(プログラミングテクニック)
stepCycle += (characterController.velocity.magnitude + (speed * (isWalking ? 1f : runStepLengthen))) * Time.deltaTime;
}
if(!(stepCycle > nextStep))
{
return;
}
// 足音の発生間隔・・・>歩く時は間隔が長い。走る時は短い。
nextStep = stepCycle + stepInterval;
PlayStepSound();
}
}
【2019版】X_Mission(基礎/全51回)
他のコースを見る足音の実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(AudioSource))]
public class PlayerController : MonoBehaviour
{
public float walkSpeed;
public float runSpeed;
private Vector3 movement;
private CharacterController characterController;
private AudioSource audioSource;
public GameObject FPSCamera;
private Vector3 moveDir = Vector3.zero;
private float gravity = 9.8f;
public float jumpPower;
public AudioClip jumpSound;
// ★追加(足音)
private float runStepLengthen = 0.7f;
private float stepInterval = 10f;
private float stepCycle;
private float nextStep;
private float speed;
private bool isWalking;
public AudioClip stepSound;
private void Start()
{
characterController = GetComponent<CharacterController>();
audioSource = GetComponent<AudioSource>();
}
private void Update()
{
PlayerMove();
// ★追加(足音)
ProgressStepCycle(speed);
}
void PlayerMove()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
movement = new Vector3(moveH, 0, moveV);
Vector3 desiredMove = FPSCamera.transform.forward * movement.z + FPSCamera.transform.right * movement.x;
moveDir.x = desiredMove.x * 5f;
moveDir.z = desiredMove.z * 5f;
if (Input.GetKey(KeyCode.LeftShift))
{
characterController.Move(moveDir * Time.deltaTime * runSpeed);
// ★追加(足音)
isWalking = false;
speed = runSpeed;
}
else
{
characterController.Move(moveDir * Time.deltaTime * walkSpeed);
// ★追加(足音)
isWalking = true;
speed = walkSpeed;
}
if (characterController.isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space))
{
moveDir.y = jumpPower;
audioSource.clip = jumpSound;
audioSource.Play();
}
}
else
{
moveDir.y -= gravity * Time.deltaTime;
}
}
// ★追加(足音)
void PlayStepSound()
{
// ジャンプ中は足音を発生させない。
if(!characterController.isGrounded)
{
return;
}
audioSource.clip = stepSound;
audioSource.PlayOneShot(audioSource.clip);
}
// ★追加(足音)
// ステップサイクル
void ProgressStepCycle(float speed)
{
if(characterController.velocity.sqrMagnitude > 0 && (movement.x != 0 || movement.z != 0))
{
// 三項演算子(プログラミングテクニック)
stepCycle += (characterController.velocity.magnitude + (speed * (isWalking ? 1f : runStepLengthen))) * Time.deltaTime;
}
if(!(stepCycle > nextStep))
{
return;
}
// 足音の発生間隔・・・>歩く時は間隔が長い。走る時は短い。
nextStep = stepCycle + stepInterval;
PlayStepSound();
}
}
移動時の足音の実装