Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Tank Model with Unity

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of "how to realize the tank model with Unity". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to realize the tank model with Unity" can help you solve the problem.

After building the appearance of the tank with cubes and cylinders, we mainly realize the up and down movement of the barrel, the firing of the shell and the movement of the shell.

Tank model

Adjustable barrel

Functional objective: click the mouse to move up and down to control the up and down movement of the gun barrel.

In fact, it is essentially to achieve the rotation of an object, but the problem is that the axis of rotation. Selecting the barrel, we can see that the axis of its rotation is the center, but the rotation of the barrel must be centered on the lower end of the cylinder. In order to solve this problem, an empty object (Cylinder_father) is added, which is located at the lower end of the barrel cylinder (that is, the rotation axis of the barrel target), and the cylinder barrel is placed under it, so that the two become a father-son relationship, so that the axis of rotation of the object can be changed indirectly.

The code is mounted on the Cylinder_father to control the rotation of the object, thus indirectly controlling the up and down movement of the barrel.

Public class Tank_rotate: MonoBehaviour {private float offsetY = 0; public float rotatespeed = 6f; / / public GameObject firepoint; / / Update is called once per frame void Update () {/ / Debug.Log (transform.rotation.eulerAngles.z); / / Fort rotation if (Input.GetMouseButton (0)) / / Mouse click {offsetY = Input.GetAxis ("Mouse Y") / / Mouse movement vector / / Debug.Log (offsetY); if (transform.rotation.eulerAngles.z > 283 & & transform.rotation.eulerAngles.z)

< 324) { transform.Rotate(new Vector3(0, 0, offsetY ) * rotatespeed, Space.Self); //firepoint.transform.Translate(new Vector3(0, -offsetX, offsetY) * rotatespeed, Space.World); } else if (transform.rotation.eulerAngles.z 0) { transform.Rotate(new Vector3(0, 0, offsetY) * rotatespeed, Space.Self); } } else { if(offsetY < 0) { transform.Rotate(new Vector3(0, 0, offsetY) * rotatespeed, Space.Self); } } } }}炮弹发射点 功能目标:按下攻击键,炮弹要从炮筒的上端点发射出去。 需要用一个物体标记发射点的位置,更重要的是,炮弹发射点的位置一定要跟随炮筒的移动而移动。因此,新增一个空物体表示发射点(firepoint),并且将发射点移到炮筒下面,形成父子关系。 在firepoint上面挂载代码,实现发射炮弹的功能。按下J键,在发射点的位置实例化生成一个预制体炮弹。 public class Shoot : MonoBehaviour{ private Transform firepoint;//发射地点 public GameObject s; private Rigidbody shell;//炮弹 private float nextfire = 1f; public float firerate = 2f; // Start is called before the first frame update void Start() { firepoint = gameObject.GetComponent();//发射点位置 shell = s.GetComponent(); } // Update is called once per frame void Update() { //点击左键并且时间已经大于发射时间 if (Input.GetKeyDown(KeyCode.J)&&Time.time>

Nextfire) / / attack key-Keyboard J {nextfire = Time.time + firerate; / / instantiate the shell Rigidbody clone; clone = (Rigidbody) Instantiate (shell, firepoint.position, shell.transform.rotation);}} the parabolic motion of the shell

Target function: after firing, the shell should follow the trajectory of the parabola.

Here is actually a simple oblique throwing motion, the key point is to decompose the velocity into the vertical direction (Y axis) and the horizontal direction (Z axis), and then simulate the uniform acceleration motion in the vertical direction, and the uniform motion in the horizontal direction. The decomposition of velocity is also very simple, which can be realized by trigonometric function. Note: trigonometric functions in Unity must be converted to radians.

The code is mounted on the prefabricated shell to realize the parabolic motion.

Public class SheelMove: MonoBehaviour {private Transform Cylinder_father; private float angle = 0; public float speed = 10f; private float speedY = 0; private float speedZ = 0; public float g = 1f; private bool flag = true; void Start () {Cylinder_father = GameObject.Find ("Cylinder_father"). Transform; angle = Cylinder_father.eulerAngles.z-360 / / the angle of the barrel, that is, the angle of velocity decomposition speedY = (- 1) * speed * Mathf.Cos ((- 1) * angle * Mathf.PI / 180); / / Radian conversion speedZ = speed * Mathf.Sin (angle * Mathf.PI / 180) / / Radian system conversion} / / Update is called once per frame void Update () {if (speedY > 0&&flag) {speedY-= g * Time.deltaTime;// Vertical Simulation of uniform acceleration transform.Translate (0cne speedYroomTime, speedZ * Time.deltaTime);} else {flag = false SpeedY + = g * Time.deltaTime;// vertical simulation of uniformly accelerated motion transform.Translate (0,-speedY * Time.deltaTime, speedZ * Time.deltaTime);} if (this.transform.position.y

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report