(スクリプト)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMovement2_Velocity : MonoBehaviour
{
public float turnSpeed;
private Rigidbody rb;
private float turnInputValue;
// ★音の切り替え
public AudioSource audioSource;
public AudioClip[] sounds;
// ★★キャタピラを動かす
public GameObject cata;
void Start()
{
rb = GetComponent<Rigidbody>();
audioSource.clip = sounds[0];
audioSource.Play();
}
void Update()
{
// 前進
if(Input.GetKey(KeyCode.LeftShift))
{
rb.velocity += transform.forward * 0.3f;
// ★★キャタピラを動かす
float offset = Time.time * 1.5f;
cata.GetComponent<meshrenderer>().material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
// ★音の切り替え
if (audioSource.clip == sounds[0])
{
audioSource.clip = sounds[1];
audioSource.Play();
}
}
else
{
// ★音の切り替え
if (audioSource.clip == sounds[1])
{
audioSource.clip = sounds[0];
audioSource.Play();
}
}
// 後退
if (Input.GetKey(KeyCode.Z))
{
rb.velocity += transform.forward * -0.2f;
// ★★キャタピラを動かす
float offset = Time.time * 1.2f;
cata.GetComponent<meshrenderer>().material.SetTextureOffset("_MainTex", new Vector2(-offset, 0));
// ★音の切り替え
if (audioSource.clip == sounds[0])
{
audioSource.clip = sounds[2];
audioSource.Play();
}
}
else
{
// ★音の切り替え
if (audioSource.clip == sounds[2])
{
audioSource.clip = sounds[0];
audioSource.Play();
}
}
TankTurn();
}
void TankTurn()
{
turnInputValue = Input.GetAxis("Horizontal");
float turn = turnInputValue * turnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
}