u3d 火炮

枪口 与子弹旋转同样角度。


离子系统与子弹 拖入地底下隐藏。


坦克:分5格动画,绑定脚本optplayer


炮塔:绑定脚本optpaota


子弹:动态绑定脚本zidanfei  addcomponent


枪口:创建一个空对象


爆炸:离子系统


opt坦克脚本:

  using UnityEngine;
using System.Collections;


public class optPlayer : MonoBehaviour
{


    public Animation player;
    private float speed = 15;
    private float angleY = 0;


    private GameObject bullet;
    private GameObject qiangkou;






    // Use this for initialization
    void Start()
    {


        player.Play("stand");


        bullet = GameObject.Find("zidan");
        qiangkou = GameObject.Find("qiangkou");


    }


    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            player.Play("forward");
        }
        else if (Input.GetKeyUp(KeyCode.W))
        {
            player.Play("stand");
        }


        if (Input.GetKeyDown(KeyCode.S))
        {
            player.Play("back");
        }
        else if (Input.GetKeyUp(KeyCode.S))
        {
            player.Play("stand");
        }




        if (Input.GetKeyDown(KeyCode.A))
        {
            player.Play("left");
        }
        else if (Input.GetKeyUp(KeyCode.A))
        {
            player.Play("stand");
        }


        if (Input.GetKeyDown(KeyCode.D))
        {
            player.Play("right");
        }
        else if (Input.GetKeyUp(KeyCode.D))
        {
            player.Play("stand");
        }




        if (Input.GetKey(KeyCode.W))
        {
            this.transform.Translate(Vector3.forward * Time.deltaTime * speed);
        }


        else if (Input.GetKey(KeyCode.S))
        {
            this.transform.Translate(Vector3.back * Time.deltaTime * speed);
        }
        else if (Input.GetKey(KeyCode.A))
        {
            this.transform.Translate(Vector3.left * Time.deltaTime * speed);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            this.transform.Translate(Vector3.right * Time.deltaTime * speed);
        }




        if (Input.GetMouseButton(0))
        {
            angleY = Input.mousePosition.x;
            this.transform.localEulerAngles = new Vector3(this.transform.localEulerAngles.x, angleY, this.transform.localEulerAngles.z);
            //print(Input.mousePosition);
        }
    }








    void LateUpdate()
    {
        if (Input.GetMouseButtonDown(1))
        {


            GameObject zidan = (GameObject)GameObject.Instantiate(bullet);
            zidan.transform.position = qiangkou.transform.position;
            zidan.transform.rotation = qiangkou.transform.rotation;
            zidan.AddComponent("Rigidbody");


            Rigidbody rigBody =  (Rigidbody)zidan.GetComponent("Rigidbody");
            rigBody.useGravity = false;




            zidan.AddComponent("zidanfei");
        }
    }
}


opt炮塔脚本:

  using UnityEngine;
using System.Collections;


public class optpaota : MonoBehaviour
{


    public GameObject target = null;
    private float angle;
    private float curAngle = 0;
    private float rotateSpeed = 1;


    void Start()
    {


    }


    // Update is called once per frame
    void Update()
    {
        //this.transform.LookAt(target.transform);
        float dx = target.transform.position.x - this.transform.position.x;
        float dz = target.transform.position.z - this.transform.position.z;


        float hudu = Mathf.Atan2(dx, dz);
        angle = hudu * 180 / Mathf.PI;
        print(angle);




        if (curAngle > 0 && angle < 0)
        {
            curAngle = angle;
        }
        else if (curAngle < 0 && angle > 0)
        {
            curAngle = angle;
        }




        if (curAngle < angle)
        {
            curAngle += rotateSpeed;
            if (curAngle >= angle)
            {
                curAngle = angle;
            }
        }
        else if (curAngle > angle)
        {
            curAngle -= rotateSpeed;
            if (curAngle <= angle)
            {
                curAngle = angle;
            }
        }


       


        this.transform.localEulerAngles = new Vector3(0, curAngle, 0);


    }


}

opt子弹脚本:

 using UnityEngine;
using System.Collections;


public class zidanfei : MonoBehaviour
{


    private int flag = 0;
    private static GameObject Baozha = null;
    // Use this for initialization
    void Start()
    {
        if (Baozha==null)
        {
            Baozha = GameObject.Find("baozha");
        }      
    }


    // 子弹飞
    void Update()
    {
        if (flag == 0)
        {
            this.transform.Translate(Vector3.up * Time.deltaTime * 20);
        }
    }




    void OnCollisionEnter(Collision colision)
    {
        flag = 1;


        GameObject baozha = (GameObject)GameObject.Instantiate(Baozha);
        baozha.transform.position = this.transform.position;


        Destroy(baozha, 0.5f);
        Destroy(gameObject);
    }


    //IEnumerator OnCollisionStay(Collision colisionInfo)
    //{
         
    //    flag = 1;


    //    yield return new WaitForSeconds(1);
    //    Destroy(gameObject);
    //}
}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值