Tankアセットへ切り替え&シンプルに動かす
![Bb15dca3 5cdd 4ca7 8fd1 c4d79edb8689](https://codegenius.org/uploads/slide/image/7177/bb15dca3-5cdd-4ca7-8fd1-c4d79edb8689.jpeg)
![2bd332f9 eb0d 47c1 9310 a686f2a1dd19](https://codegenius.org/uploads/slide/image/7178/2bd332f9-eb0d-47c1-9310-a686f2a1dd19.jpeg)
![310fe0bf 6aee 4ad8 9bdf ff2cae62ec12](https://codegenius.org/uploads/slide/image/7179/310fe0bf-6aee-4ad8-9bdf-ff2cae62ec12.jpeg)
![F419b3b5 9357 4d3d 8b0c 752aaf53457f](https://codegenius.org/uploads/slide/image/7180/f419b3b5-9357-4d3d-8b0c-752aaf53457f.jpeg)
![E96dbae7 ab45 40d0 802b fb52df22feb4](https://codegenius.org/uploads/slide/image/7181/e96dbae7-ab45-40d0-802b-fb52df22feb4.jpeg)
Tankを動かす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★コンポーネントの自動追加
// Rigidbodyを自動的に追加してくれる。
[RequireComponent(typeof(Rigidbody))]
public class TankMovement_X : MonoBehaviour
{
private float turnInputValue;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
// 前進(Wキー押しで前進)
if(Input.GetKey(KeyCode.W))
{
rb.velocity += transform.forward * 0.4f; // 前進速度は自由に変更可能
}
// 前進(Sキー押しで後退)
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);
}
}
![33fb2cd3 d2bf 4e0c b5ff e36a354c4713](https://codegenius.org/uploads/slide/image/7182/33fb2cd3-d2bf-4e0c-b5ff-e36a354c4713.jpeg)
![9ccb78c5 2153 4670 aeb6 76be073628ae](https://codegenius.org/uploads/slide/image/7183/9ccb78c5-2153-4670-aeb6-76be073628ae.jpeg)
![B0093589 e392 49ed b2dc 9c63c7f1c424](https://codegenius.org/uploads/slide/image/7184/b0093589-e392-49ed-b2dc-9c63c7f1c424.jpeg)
![Bb15dca3 5cdd 4ca7 8fd1 c4d79edb8689](https://codegenius.org/uploads/slide/image/7177/bb15dca3-5cdd-4ca7-8fd1-c4d79edb8689.jpeg)
![2bd332f9 eb0d 47c1 9310 a686f2a1dd19](https://codegenius.org/uploads/slide/image/7178/2bd332f9-eb0d-47c1-9310-a686f2a1dd19.jpeg)
![310fe0bf 6aee 4ad8 9bdf ff2cae62ec12](https://codegenius.org/uploads/slide/image/7179/310fe0bf-6aee-4ad8-9bdf-ff2cae62ec12.jpeg)
![F419b3b5 9357 4d3d 8b0c 752aaf53457f](https://codegenius.org/uploads/slide/image/7180/f419b3b5-9357-4d3d-8b0c-752aaf53457f.jpeg)
![E96dbae7 ab45 40d0 802b fb52df22feb4](https://codegenius.org/uploads/slide/image/7181/e96dbae7-ab45-40d0-802b-fb52df22feb4.jpeg)
Tankを動かす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★コンポーネントの自動追加
// Rigidbodyを自動的に追加してくれる。
[RequireComponent(typeof(Rigidbody))]
public class TankMovement_X : MonoBehaviour
{
private float turnInputValue;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
// 前進(Wキー押しで前進)
if(Input.GetKey(KeyCode.W))
{
rb.velocity += transform.forward * 0.4f; // 前進速度は自由に変更可能
}
// 前進(Sキー押しで後退)
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);
}
}
![33fb2cd3 d2bf 4e0c b5ff e36a354c4713](https://codegenius.org/uploads/slide/image/7182/33fb2cd3-d2bf-4e0c-b5ff-e36a354c4713.jpeg)
![9ccb78c5 2153 4670 aeb6 76be073628ae](https://codegenius.org/uploads/slide/image/7183/9ccb78c5-2153-4670-aeb6-76be073628ae.jpeg)
![B0093589 e392 49ed b2dc 9c63c7f1c424](https://codegenius.org/uploads/slide/image/7184/b0093589-e392-49ed-b2dc-9c63c7f1c424.jpeg)
Tankアセットへ切り替え&シンプルに動かす