Tankのキャタピラを動かす
キャタピラを動かす
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);
}
}
キャタピラを動かす
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);
}
}
Tankのキャタピラを動かす