(スクリプト)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMovement_1 : MonoBehaviour
{
private float moveS;
public float turnS;
private Rigidbody rb;
private float movementInputValue;
private float turnInputValue;
// ★音の切り替え
public AudioSource audioSource;
public AudioClip[] souds;
private void Awake()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
TnakMove();
TankTurn();
// ★音の切り替え
EngineSound();
}
void TnakMove()
{
movementInputValue = Input.GetAxis("Vertical");
Vector3 movement = transform.forward * movementInputValue * moveS * Time.deltaTime;
rb.MovePosition(rb.position + movement);
var inputValue = movementInputValue * movementInputValue;
if(inputValue == 1)
{
moveS += Time.deltaTime * 2;
print(moveS);
if(moveS > 7)
{
moveS = 7;
}
}
if(inputValue == 0)
{
moveS = 0;
}
}
void TankTurn()
{
turnInputValue = Input.GetAxis("Horizontal");
float turn = turnInputValue * turnS * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
// ★音の切り替え
void EngineSound()
{
// Mathf.Absは絶対値を返す
if (Mathf.Abs(movementInputValue) < 0.1f && Mathf.Abs(turnInputValue) < 0.1f)
{
if (audioSource.clip == souds[1])
{
// アイドリング音に変更
audioSource.clip = souds[0];
audioSource.Play();
}
}
else
{
if (audioSource.clip == souds[0])
{
// 走行音に変更
audioSource.clip = souds[1];
audioSource.Play();
}
}
}
}
(設定)
