首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >仅当触摸按钮而不是整个屏幕时,才应射击

仅当触摸按钮而不是整个屏幕时,才应射击
EN

Stack Overflow用户
提问于 2019-05-15 21:40:26
回答 1查看 38关注 0票数 0

我做了一个3d游戏,我有一个第一人称控制器,由操纵杆控制。我希望我的播放器只有在我触摸屏幕上的按钮时才能拍摄,但现在我的播放器在我触摸屏幕上的任何东西时都会拍摄。以下是拍摄函数的脚本:

代码语言:javascript
复制
using UnityEngine;
using UnityEngine.UI;

public class Gun : MonoBehaviour
{
    public float damage = 10f;
    public float range = 100f;
    public Camera fpsCam;
    public Button button;

    // Start is called before the first frame update
    void Start()
    {
        //Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
       button.onClick.AddListener(TaskOnClick);
    }

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

    {
        Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
        Debug.DrawRay(transform.position, forward, Color.green);


    }
    void Shoot()
    {
        RaycastHit hit;
       if( Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
            Debug.Log(hit.transform.name);
            Target target = hit.transform.GetComponent<Target>();
            if(target != null)
            {
                target.TakeDamage(damage);
            }
        }
    }
    void OnCollisionEnter(Collision collision)
    {
        Debug.DrawRay(collision.contacts[0].point, collision.contacts[0].normal, Color.green, 2, false);

    }


    void TaskOnClick()
    {

            Shoot();


    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56150831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档