好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Unity实现坦克模型

本文实例为大家分享了Unity实现坦克模型的具体代码,供大家参考,具体内容如下

用立方体和圆柱体搭建完坦克的外观,接下来我们主要实现炮筒的上下移动、炮弹的发射和炮弹的运动。

坦克模型

可调节的炮筒

功能目标: 鼠标单击上下移动以控制炮筒的上下移动。 其实本质上就是实现一个物体的旋转,但是问题在于,旋转的轴心。选中炮筒,我们可以看到,它旋转的轴心就是中心,但是炮筒的旋转肯定是要以圆柱体的下端点为轴心。为了解决这个问题,增加一个空物体(Cylinder_father),位置在炮筒圆柱体的下端点(即炮筒目标的旋转轴心),并且把圆柱体炮筒放在其下面,使二者成为父子关系,这样也就可以间接的改变物体旋转的轴心。

在Cylinder_father上面挂载代码控制物体的旋转,以此间接的控制炮筒的上下移动。

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); ? ? ? ? //炮台的旋转 ? ? ? ? if (Input.GetMouseButton(0))//鼠标单击 ? ? ? ? { ? ? ? ? ? ? offsetY = Input.GetAxis("Mouse Y");//鼠标的移动向量 ? ? ? ? ? ? //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 <= 283) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if(offsetY > 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<Transform>();//发射点位置 ? ? ? ? shell = s.GetComponent<Rigidbody>(); ? ? } ? ? // Update is called once per frame ? ? void Update() ? ? { ? ? ? ? //点击左键并且时间已经大于发射时间 ? ? ? ? if (Input.GetKeyDown(KeyCode.J)&&Time.time>nextfire )//攻击键——键盘J ? ? ? ? { ? ? ? ? ? ? nextfire = Time.time + firerate; ? ? ? ? ? ? //实例化炮弹 ? ? ? ? ? ? Rigidbody clone; ? ? ? ? ? ? clone = (Rigidbody)Instantiate(shell, firepoint.position, shell.transform.rotation); ? ? ? ? } ? ? } }

炮弹的抛物线运动

目标功能: 炮弹发射后要按照抛物线的轨迹运动。 这里其实就是一个简单的斜抛运动,关键点就在于将速度分解到竖直方向(Y轴)和水平方向(Z轴),然后竖直方向上模拟匀加速运动,水平方向上是匀速运动。速度的分解也很简单,利用三角函数就可以实现。 注意:Unity中的三角函数必须要转化成为弧度制。 在预制体炮弹上挂载代码,实现抛物线运动。

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;//炮筒的角度也即速度分解的角度 ? ? ? ? speedY = (-1)*speed * Mathf.Cos((-1)*angle * Mathf.PI / 180);//弧度制转换 ? ? ? ? speedZ = speed * Mathf.Sin(angle * Mathf.PI / 180);//弧度制转换 ? ? } ? ? // Update is called once per frame ? ? void Update() ? ? { ? ? ? ? if (speedY>0&&flag) ? ? ? ? { ? ? ? ? ? ? speedY -= g * Time.deltaTime;//竖直方向上模拟匀加速运动 ? ? ? ? ? ? transform.Translate(0,speedY*Time.deltaTime, speedZ * Time.deltaTime); ? ? ? ? } ? ? ? ? else ? ? ? ? { ? ? ? ? ? ? flag = false; ? ? ? ? ? ? speedY += g * Time.deltaTime;//竖直方向上模拟匀加速运动 ? ? ? ? ? ? transform.Translate(0, -speedY * Time.deltaTime, speedZ * Time.deltaTime); ? ? ? ? } ? ? ? ? if(this.transform.position.y<0)//如果炮弹运动到地面以下,毁灭 ? ? ? ? { ? ? ? ? ? ? Destroy(this.gameObject); ? ? ? ? } ? ? } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

查看更多关于Unity实现坦克模型的详细内容...

  阅读:44次