戦車を動かす(前進・後退・旋回)
戦車を前進・後退・旋回させる
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);
}
}
BattleTank(基礎/全31回)
他のコースを見る戦車を前進・後退・旋回させる
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);
}
}
戦車を動かす(前進・後退・旋回)