アイドル音とドライブ音を出す

アイドル音とドライブ音の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class TankMovement_X : MonoBehaviour
{
private float turnInputValue;
private Rigidbody rb;
// ★サウンド
public AudioClip[] sounds;
private AudioSource audioSource;
void Start()
{
rb = GetComponent<Rigidbody>();
// ★サウンド
audioSource = GetComponent<AudioSource>();
audioSource.clip = sounds[0]; // アイドル音
audioSource.Play();
}
void Update()
{
// ★サウンド(音の切り替え)
// 「GetKey」だとうまくいかないので注意!
if(Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.S))
{
audioSource.clip = sounds[1]; // ドライブ音
audioSource.Play();
}
// ★サウンド
if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S))
{
audioSource.clip = sounds[0]; // アイドル時の音
audioSource.Play();
}
if (Input.GetKey(KeyCode.W))
{
rb.velocity += transform.forward * 0.4f;
}
if (Input.GetKey(KeyCode.S))
{
rb.velocity -= transform.forward * 0.4f;
}
TankTurn();
}
void TankTurn()
{
turnInputValue = Input.GetAxis("Horizontal");
float turn = turnInputValue * Time.deltaTime * 50;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
}





アイドル音とドライブ音の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class TankMovement_X : MonoBehaviour
{
private float turnInputValue;
private Rigidbody rb;
// ★サウンド
public AudioClip[] sounds;
private AudioSource audioSource;
void Start()
{
rb = GetComponent<Rigidbody>();
// ★サウンド
audioSource = GetComponent<AudioSource>();
audioSource.clip = sounds[0]; // アイドル音
audioSource.Play();
}
void Update()
{
// ★サウンド(音の切り替え)
// 「GetKey」だとうまくいかないので注意!
if(Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.S))
{
audioSource.clip = sounds[1]; // ドライブ音
audioSource.Play();
}
// ★サウンド
if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S))
{
audioSource.clip = sounds[0]; // アイドル時の音
audioSource.Play();
}
if (Input.GetKey(KeyCode.W))
{
rb.velocity += transform.forward * 0.4f;
}
if (Input.GetKey(KeyCode.S))
{
rb.velocity -= transform.forward * 0.4f;
}
TankTurn();
}
void TankTurn()
{
turnInputValue = Input.GetAxis("Horizontal");
float turn = turnInputValue * Time.deltaTime * 50;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
}




アイドル音とドライブ音を出す