Tankのキャタピラを動かす
![17c97011 dd8d 4f32 a3bc b20813cf21bb](https://codegenius.org/uploads/slide/image/7190/17c97011-dd8d-4f32-a3bc-b20813cf21bb.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;
// ★キャタピラ
public GameObject[] cata;
void Start()
{
rb = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
audioSource.clip = sounds[0];
audioSource.Play();
}
void Update()
{
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;
// ★キャタピラ
float offset = Time.time * 1.2f;
foreach(GameObject c in cata)
{
c.GetComponent<MeshRenderer>().material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
}
}
if (Input.GetKey(KeyCode.S))
{
rb.velocity -= transform.forward * 0.4f;
// ★キャタピラ
float offset = Time.time * 1.2f;
foreach (GameObject c in cata)
{
c.GetComponent<MeshRenderer>().material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
}
}
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);
}
}
![870b92f2 8b36 4873 86bf adb3db96b081](https://codegenius.org/uploads/slide/image/7191/870b92f2-8b36-4873-86bf-adb3db96b081.jpeg)
![0d74122c 13a6 432b 9738 65cdf9b65a7a](https://codegenius.org/uploads/slide/image/7192/0d74122c-13a6-432b-9738-65cdf9b65a7a.jpeg)
![17c97011 dd8d 4f32 a3bc b20813cf21bb](https://codegenius.org/uploads/slide/image/7190/17c97011-dd8d-4f32-a3bc-b20813cf21bb.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;
// ★キャタピラ
public GameObject[] cata;
void Start()
{
rb = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
audioSource.clip = sounds[0];
audioSource.Play();
}
void Update()
{
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;
// ★キャタピラ
float offset = Time.time * 1.2f;
foreach(GameObject c in cata)
{
c.GetComponent<MeshRenderer>().material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
}
}
if (Input.GetKey(KeyCode.S))
{
rb.velocity -= transform.forward * 0.4f;
// ★キャタピラ
float offset = Time.time * 1.2f;
foreach (GameObject c in cata)
{
c.GetComponent<MeshRenderer>().material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
}
}
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);
}
}
![870b92f2 8b36 4873 86bf adb3db96b081](https://codegenius.org/uploads/slide/image/7191/870b92f2-8b36-4873-86bf-adb3db96b081.jpeg)
![0d74122c 13a6 432b 9738 65cdf9b65a7a](https://codegenius.org/uploads/slide/image/7192/0d74122c-13a6-432b-9738-65cdf9b65a7a.jpeg)
Tankのキャタピラを動かす