2台の戦車を個別に動かす
![52139880 1f57 424a 8c71 c27083d51569](https://codegenius.org/uploads/slide/image/4325/52139880-1f57-424a-8c71-c27083d51569.jpeg)
![83f69b28 b086 46c3 b8c6 f8abadbc6553](https://codegenius.org/uploads/slide/image/4326/83f69b28-b086-46c3-b8c6-f8abadbc6553.jpeg)
![42065050 9780 47ce b85b 345ffc649004](https://codegenius.org/uploads/slide/image/4327/42065050-9780-47ce-b85b-345ffc649004.jpeg)
戦車を個別に動かす
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);
}
}
![47f9ad44 41b3 4528 be3c c9a14fd691a1](https://codegenius.org/uploads/slide/image/4328/47f9ad44-41b3-4528-be3c-c9a14fd691a1.jpeg)
![D60ffcae 4f93 41c8 b5db db1c2dfe11ed](https://codegenius.org/uploads/slide/image/4329/d60ffcae-4f93-41c8-b5db-db1c2dfe11ed.jpeg)
![F244e715 8b0a 4589 8ea7 a956c6849034](https://codegenius.org/uploads/slide/image/4330/f244e715-8b0a-4589-8ea7-a956c6849034.jpeg)
![A4b37e24 27ee 4b1c b47f 61dcefc84ffc](https://codegenius.org/uploads/slide/image/4331/a4b37e24-27ee-4b1c-b47f-61dcefc84ffc.jpeg)
![F9405ed5 d30f 45ec 91ae 7ff36aaa1224](https://codegenius.org/uploads/slide/image/4332/f9405ed5-d30f-45ec-91ae-7ff36aaa1224.jpeg)
![52139880 1f57 424a 8c71 c27083d51569](https://codegenius.org/uploads/slide/image/4325/52139880-1f57-424a-8c71-c27083d51569.jpeg)
![83f69b28 b086 46c3 b8c6 f8abadbc6553](https://codegenius.org/uploads/slide/image/4326/83f69b28-b086-46c3-b8c6-f8abadbc6553.jpeg)
![42065050 9780 47ce b85b 345ffc649004](https://codegenius.org/uploads/slide/image/4327/42065050-9780-47ce-b85b-345ffc649004.jpeg)
戦車を個別に動かす
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);
}
}
![47f9ad44 41b3 4528 be3c c9a14fd691a1](https://codegenius.org/uploads/slide/image/4328/47f9ad44-41b3-4528-be3c-c9a14fd691a1.jpeg)
![D60ffcae 4f93 41c8 b5db db1c2dfe11ed](https://codegenius.org/uploads/slide/image/4329/d60ffcae-4f93-41c8-b5db-db1c2dfe11ed.jpeg)
![F244e715 8b0a 4589 8ea7 a956c6849034](https://codegenius.org/uploads/slide/image/4330/f244e715-8b0a-4589-8ea7-a956c6849034.jpeg)
![A4b37e24 27ee 4b1c b47f 61dcefc84ffc](https://codegenius.org/uploads/slide/image/4331/a4b37e24-27ee-4b1c-b47f-61dcefc84ffc.jpeg)
![F9405ed5 d30f 45ec 91ae 7ff36aaa1224](https://codegenius.org/uploads/slide/image/4332/f9405ed5-d30f-45ec-91ae-7ff36aaa1224.jpeg)
2台の戦車を個別に動かす