2台の戦車を個別に動かす



戦車を個別に動かす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMovement : MonoBehaviour
{
// (ポイント)2台の戦車にIDを割り当てる。
public string playerName;
private float moveSpeed = 5;
private float turnSpeed = 100;
private Rigidbody rb;
private float movementInputValue;
private float turnInputValue;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
TankMove();
TankTurn();
}
void TankMove()
{
// (ポイント)Verticalの後ろにIDを追加する。
movementInputValue = Input.GetAxis("Vertical" + playerName);
Vector3 movement = transform.forward * movementInputValue * moveSpeed * Time.deltaTime;
rb.MovePosition(rb.position + movement);
}
void TankTurn()
{
// (ポイント)Horizontalの後ろにIDを追加する。
turnInputValue = Input.GetAxis("Horizontal" + playerName);
float turn = turnInputValue * turnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
}








戦車を個別に動かす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMovement : MonoBehaviour
{
// (ポイント)2台の戦車にIDを割り当てる。
public string playerName;
private float moveSpeed = 5;
private float turnSpeed = 100;
private Rigidbody rb;
private float movementInputValue;
private float turnInputValue;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
TankMove();
TankTurn();
}
void TankMove()
{
// (ポイント)Verticalの後ろにIDを追加する。
movementInputValue = Input.GetAxis("Vertical" + playerName);
Vector3 movement = transform.forward * movementInputValue * moveSpeed * Time.deltaTime;
rb.MovePosition(rb.position + movement);
}
void TankTurn()
{
// (ポイント)Horizontalの後ろにIDを追加する。
turnInputValue = Input.GetAxis("Horizontal" + playerName);
float turn = turnInputValue * turnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
rb.MoveRotation(rb.rotation * turnRotation);
}
}





2台の戦車を個別に動かす