戦車を動かす(前進・後退・旋回)
![Fa0a6be9 e071 4626 a962 fb0d2563fe1b](https://codegenius.org/uploads/slide/image/1397/fa0a6be9-e071-4626-a962-fb0d2563fe1b.jpeg)
戦車を前進・後退・旋回させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMovement : MonoBehaviour
{
public float moveSpeed;
public float turnSpeed;
private Rigidbody rb;
private float movementInputValue;
private float turnInputValue;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
Move();
Turn();
}
// 前進・後退のメソッド
void Move()
{
movementInputValue = Input.GetAxis("Vertical");
Vector3 movement = transform.forward * movementInputValue * moveSpeed * Time.deltaTime;
rb.MovePosition(rb.position + movement);
}
// 旋回のメソッド
void Turn()
{
turnInputValue = Input.GetAxis("Horizontal");
float turn = turnInputValue * turnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
}
![E14c489d 9855 49a8 aa5a 375bab3499ec](https://codegenius.org/uploads/slide/image/1398/e14c489d-9855-49a8-aa5a-375bab3499ec.jpeg)
![E448fada b6b0 40c3 b0c9 05265bffe0ce](https://codegenius.org/uploads/slide/image/1399/e448fada-b6b0-40c3-b0c9-05265bffe0ce.jpeg)
![6f49ef85 c7db 41f0 b64a 5bded7ed22c2](https://codegenius.org/uploads/slide/image/1400/6f49ef85-c7db-41f0-b64a-5bded7ed22c2.jpeg)
BattleTank(基礎/全31回)
他のコースを見る![Fa0a6be9 e071 4626 a962 fb0d2563fe1b](https://codegenius.org/uploads/slide/image/1397/fa0a6be9-e071-4626-a962-fb0d2563fe1b.jpeg)
戦車を前進・後退・旋回させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMovement : MonoBehaviour
{
public float moveSpeed;
public float turnSpeed;
private Rigidbody rb;
private float movementInputValue;
private float turnInputValue;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
Move();
Turn();
}
// 前進・後退のメソッド
void Move()
{
movementInputValue = Input.GetAxis("Vertical");
Vector3 movement = transform.forward * movementInputValue * moveSpeed * Time.deltaTime;
rb.MovePosition(rb.position + movement);
}
// 旋回のメソッド
void Turn()
{
turnInputValue = Input.GetAxis("Horizontal");
float turn = turnInputValue * turnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
}
![E14c489d 9855 49a8 aa5a 375bab3499ec](https://codegenius.org/uploads/slide/image/1398/e14c489d-9855-49a8-aa5a-375bab3499ec.jpeg)
![E448fada b6b0 40c3 b0c9 05265bffe0ce](https://codegenius.org/uploads/slide/image/1399/e448fada-b6b0-40c3-b0c9-05265bffe0ce.jpeg)
![6f49ef85 c7db 41f0 b64a 5bded7ed22c2](https://codegenius.org/uploads/slide/image/1400/6f49ef85-c7db-41f0-b64a-5bded7ed22c2.jpeg)
戦車を動かす(前進・後退・旋回)