(Unity)velocityを使って戦車を加速させる。

(スクリプト)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TankMovement2_Velocity : MonoBehaviour
{
    public float turnSpeed;
    private Rigidbody rb;
    private float turnInputValue;
     
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
 
    void Update()
    {
        if(Input.GetKey(KeyCode.LeftShift))
        {
            rb.velocity += transform.forward * 0.3f;
        }
   
        TankTurn();
    }
 
    void TankTurn()
    {
        turnInputValue = Input.GetAxis("Horizontal");
        float turn = turnInputValue * turnSpeed * Time.deltaTime;
        Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
        rb.MoveRotation(rb.rotation * turnRotation);
    }
}