戦車を動かす(前進・後退・旋回)

戦車を動かす(前進・後退・旋回)
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()
{
TankMove();
TankTurn();
}
// 前進・後退のメソッド
void TankMove()
{
movementInputValue = Input.GetAxis("Vertical");
Vector3 movement = transform.forward * movementInputValue * moveSpeed * Time.deltaTime;
rb.MovePosition(rb.position + movement);
}
// 旋回のメソッド
void TankTurn()
{
turnInputValue = Input.GetAxis("Horizontal");
float turn = turnInputValue * turnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
}



【2019版】BattleTank(基礎/全38回)
他のコースを見る
戦車を動かす(前進・後退・旋回)
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()
{
TankMove();
TankTurn();
}
// 前進・後退のメソッド
void TankMove()
{
movementInputValue = Input.GetAxis("Vertical");
Vector3 movement = transform.forward * movementInputValue * moveSpeed * Time.deltaTime;
rb.MovePosition(rb.position + movement);
}
// 旋回のメソッド
void TankTurn()
{
turnInputValue = Input.GetAxis("Horizontal");
float turn = turnInputValue * turnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
}



戦車を動かす(前進・後退・旋回)