アイドル音とドライブ音を出す
![3184c22c 67d9 41e7 949d bee7ddebd2cf](https://codegenius.org/uploads/slide/image/7185/3184c22c-67d9-41e7-949d-bee7ddebd2cf.jpeg)
アイドル音とドライブ音の追加
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);
}
}
![23c93570 c4c2 4a97 a78a 85d9f0dba00b](https://codegenius.org/uploads/slide/image/7186/23c93570-c4c2-4a97-a78a-85d9f0dba00b.jpeg)
![337bf83f be6d 4e9a bf70 e694d2ec0c85](https://codegenius.org/uploads/slide/image/7187/337bf83f-be6d-4e9a-bf70-e694d2ec0c85.jpeg)
![C1ec233d f34c 4457 8309 1a694244f15c](https://codegenius.org/uploads/slide/image/7188/c1ec233d-f34c-4457-8309-1a694244f15c.jpeg)
![04bd291f d976 418e ba1b 21ad296d5256](https://codegenius.org/uploads/slide/image/7189/04bd291f-d976-418e-ba1b-21ad296d5256.jpeg)
![3184c22c 67d9 41e7 949d bee7ddebd2cf](https://codegenius.org/uploads/slide/image/7185/3184c22c-67d9-41e7-949d-bee7ddebd2cf.jpeg)
アイドル音とドライブ音の追加
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);
}
}
![23c93570 c4c2 4a97 a78a 85d9f0dba00b](https://codegenius.org/uploads/slide/image/7186/23c93570-c4c2-4a97-a78a-85d9f0dba00b.jpeg)
![337bf83f be6d 4e9a bf70 e694d2ec0c85](https://codegenius.org/uploads/slide/image/7187/337bf83f-be6d-4e9a-bf70-e694d2ec0c85.jpeg)
![C1ec233d f34c 4457 8309 1a694244f15c](https://codegenius.org/uploads/slide/image/7188/c1ec233d-f34c-4457-8309-1a694244f15c.jpeg)
![04bd291f d976 418e ba1b 21ad296d5256](https://codegenius.org/uploads/slide/image/7189/04bd291f-d976-418e-ba1b-21ad296d5256.jpeg)
アイドル音とドライブ音を出す