1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class Tank : MonoBehaviour
6 {
7
8 // Rightbod控制3D物体移动:
9
10 //首先定义一个速度和旋转速度
11
12 public float speed = 5f;
13
14 public float angSpeed = 5f;
15
16 //获取刚体
17
18 public Rigidbody rig;
19
20 //在Start函数中获取刚体组件
21 void Start()
22 {
23 rig = GetComponent<Rigidbody>();
24 }
25 void Update()
26 {
27 //在Update函数中,
28
29 //获取键盘垂直轴的输入(W.S.上下键)
30
31 var v = Input.GetAxis("Vertical");
32
33 //通过刚体给坦克一个速度
34
35 rig.velocity = transform.forward * v * speed;
36
37 //获取键盘水平轴的输入(A.D.左右键)
38
39
40 float h = Input.GetAxis("Horizontal");
41
42 //通过刚体给坦克一个旋转速度
43
44 rig.angularVelocity = transform.up * h * angSpeed;
45 }
46 }