简体   繁体   English

我如何让光线投射破坏游戏 object

[英]how do i get the raycast to destroy the game object

I want to destroy the object you shoot but when ever i shoot nothing happens我想摧毁你开枪的 object 但我开枪时什么也没有发生

     void Update()
        {
            if (Input.GetKeyDown(KeyCode.Mouse1))
            {

                if (Physics.Raycast(cam.position, cam.forward, 500))
                {

                    Destroy(hit.transform.gameObject);
                }
            }
        }

You are not checking if the Raycast hit something.你不是在检查 Raycast 是否击中了什么东西。 You can do it like this你可以这样做

    void Update()
    {
       if (Input.GetKeyDown(KeyCode.Mouse1))
       {

         Raycasthit hit;

         if(Physics.Raycast(cam.transform.position,cam.transform.forward,out hit,500))
         {
            Destroy(hit.transform.gameObject);
         }
       }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM