将实例化的子弹转换为摄像机中心

时间:2018-05-02 19:55:59

标签: c# unity3d unityscript

我制作了一个非常简单的枪脚本,目前子弹变换的方向与我设置为子弹生成位置的GameObject相同。但我希望子弹转向相机的中心,我有几个不同的想法。第一个是射线投影,当光线投射击中一些东西将子弹形式的子弹转换为raycasthit位置。但我不知道怎么做,有人可以帮助我吗?

这是我的剧本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour {

    public Transform bulletCapTransform;
    public GameObject bulletCap;

    public float ammo;
    public float magAmmo;

    public GameObject shootEffect;
    public Transform bulletTransform;
    public GameObject bullet;
    public float bulletForce;

    public GameObject crossHair;

    public float fireRate = 0.3333f;
    private float timeStamp;

    Animator anim;

    // Use this for initialization
    void Start ()
    {
        anim = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update ()
    {
        Shoot();
        Aim();
    }

    public void OnGUI()
    {
        GUI.Box(new Rect(10, 10, 50, 25), magAmmo + " / " + ammo);
    }

    public void Shoot()
    {
        if(Time.time >= timeStamp && Input.GetButton("Fire1") && magAmmo > 0)
        {
            GameObject bulletCapInstance;
            bulletCapInstance = Instantiate(bulletCap, bulletCapTransform.transform.position, bulletCapTransform.rotation) as GameObject;
            bulletCapInstance.GetComponent<Rigidbody>().AddForce(bulletCapTransform.right *10000);

            GameObject bulletInstance;
            bulletInstance = Instantiate(bullet, bulletTransform) as GameObject;
            bulletInstance.GetComponent<Rigidbody>().AddForce(bulletTransform.forward * bulletForce);

            Instantiate(shootEffect, bulletTransform);

            timeStamp = Time.time + fireRate;

            magAmmo = magAmmo -1;
        }

        if(Input.GetKeyDown(KeyCode.R))
        {
            //This is just for testing
            magAmmo = magAmmo + 10;
        }
    }

    void Aim()
    {
        if(Input.GetButton("Fire2"))
        {
            anim.SetBool("Aiming", true);
            crossHair.SetActive(false);
        }
        else
        {
            anim.SetBool("Aiming", false);
            crossHair.SetActive(true);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

所以我认为你想从枪中发射射弹&#34;对象位于屏幕中心的任何位置(无论中心物体距离如何)

这个剧本对我有用,并且在视图的中心射击。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour
{

public Transform bulletCapTransform;
public GameObject bulletCap;

public float ammo;
public float magAmmo;

public GameObject shootEffect;
public Transform bulletTransform;
public GameObject bullet;
public float bulletForce;

public GameObject crossHair;

public float fireRate = 0.3333f;
private float timeStamp;

Animator anim;

// Use this for initialization
void Start()
{
    anim = gameObject.GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{

   Shoot();
   Aim();

}

public void OnGUI()
{
    GUI.Box(new Rect(10, 10, 50, 25), magAmmo + " / " + ammo);
}

public void Shoot()
{
    if (Time.time >= timeStamp && Input.GetButton("Fire1") && magAmmo > 0)
    {
        Debug.Log("shoot");
        GameObject bulletCapInstance;
        bulletCapInstance = Instantiate(bulletCap, bulletCapTransform.transform.position, bulletCapTransform.rotation) as GameObject;
        bulletCapInstance.GetComponent<Rigidbody>().AddForce(bulletCapTransform.right * 10000);


        //This will send a raycast straight forward from your camera centre.
        Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        RaycastHit hit;
        //check for a hit
        if (Physics.Raycast(ray, out hit))
        {
            // take the point of collision (make sure all objects have a collider)
            Vector3 colisionPoint = hit.point;

            //Create a vector for the path of the bullet from the 'gun' to the target
            Vector3 bulletVector = colisionPoint - bullet.transform.position;

            GameObject bulletInstance = Instantiate(bullet, bulletTransform) as GameObject;

            //See it on it's way
            bulletInstance.GetComponent<Rigidbody>().AddForce(bulletVector * bulletForce);

        }


        Instantiate(shootEffect, bulletTransform);

        timeStamp = Time.time + fireRate;

        magAmmo = magAmmo - 1;
    }

    if (Input.GetKeyDown(KeyCode.R))
    {
        //This is just for testing
        magAmmo = magAmmo + 10;
    }
}

void Aim()
{
    if (Input.GetButton("Fire2"))
    {
        anim.SetBool("Aiming", true);
        crossHair.SetActive(false);
    }
    else
    {
        anim.SetBool("Aiming", false);
        crossHair.SetActive(true);
    }
}
}

要记住的关键点是:

Camera.main.ViewportPointToRay(0.5, 0.5, 0);

将从屏幕中心向前拍摄一张光线投射。

值得真正了解光线投射及其来龙去脉。一开始看起来令人生畏,但是一旦你把它弄下来它就非常有用并且非常直观。有很多很好的youtube教程。

希望你的游戏变好!

相关问题