【Unity】坦克移动和发射炮弹脚本

本篇仅用于纪念个人第一个C#脚本(实际没什么用)

(1)坦克移动操作并连接炮弹

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class p1controller : MonoBehaviour
 6 {
 7     [SerializeField] private float moveSpeed = 10.0f;
 8     [SerializeField] private float rotateSpeed = 100.0f;
 9     [SerializeField] private GameObject missile;
10     [SerializeField] private Transform shootPoint;
11     // Start is called before the first frame update
12     void Start()
13     {
14         
15     }
16 
17     // Update is called once per frame
18     void Update()
19     {
20         if(Input.GetKey(KeyCode.W))
21         {
22             this.transform.Translate(0, 0, moveSpeed * Time.deltaTime);
23         }
24         else if(Input.GetKey (KeyCode.S))
25         {
26             this.transform.Translate(0, 0, -moveSpeed * Time.deltaTime);
27         }
28         
29         if (Input.GetKey(KeyCode.A))
30         {
31             this.transform.Rotate(0, -rotateSpeed * Time.deltaTime,0);
32         }
33         else if (Input.GetKey(KeyCode.D))
34         {
35             this.transform.Rotate(0, rotateSpeed * Time.deltaTime,0);
36         }
37 
38         if(Input.GetKeyDown("h"))
39         {
40             Instantiate(missile, shootPoint.position, shootPoint.rotation);
41         }
42     }
43 }

(2)炮弹发射



 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class missile : MonoBehaviour
 6 {
 7     [SerializeField] private float speed = 2.0f;
 8     [SerializeField] private float lifeTime = 5.0f;
 9     // Start is called before the first frame update
10     void Start()
11     {
12         
13     }
14 
15     // Update is called once per frame
16     void Update()
17     {
18         transform.Translate(0, 0, speed * Time.deltaTime);
19         Destroy(this.gameObject, lifeTime);
20     }
21 }


 

上一篇:python库——sklearn


下一篇:vue-video-player 相关事件笔记