获取枪的方向,在枪口处射击

来源:互联网 发布:文件夹图标制作软件 编辑:程序博客网 时间:2024/04/29 07:28

写完之后才发现都是一屏幕中心为准星,以摄像机方向为射击方向。不过既然写都写了,说不定以后还会用到,就传上来吧。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour {
    public GameObject buttle;
    private Vector3 rotate;
    private Vector3 dir;
    public Transform pos;
    // Use this for initialization
    void Start () {
    }

// Update is called once per frame
void Update () {
        rotate = this.transform.rotation.eulerAngles;
        float a = Mathf.Cos(Mathf.PI * rotate.x / 180) * Mathf.Sin(Mathf.PI * rotate.y / 180);
        float b = -Mathf.Sin(Mathf.PI * rotate.x / 180);
        float c = Mathf.Sqrt(1 - a * a - b * b);
        if(rotate.y>90&&rotate.y<270)
        {
            c = -c;
        }
        Debug.Log(rotate);


        dir = new Vector3(a, b, c);
        Debug.Log(dir); 
        if (Input.GetButtonDown("Fire1"))
        {
            GameObject buttle1= Instantiate(buttle,pos.position,
                Quaternion.identity);
            buttle1.GetComponent<Rigidbody>().velocity =dir*5000*Time.deltaTime;
            Debug.Log("Shoot!");
        }
        
        
    }
}

0 0