我有一个脚本,可以在游戏中任意的位置产卵猫,当用户点击它们时,它们应该被销毁。然而,我的剧本出了问题,我想知道有没有人知道光线播报有什么问题?
public void CatClick () {
if (Input.GetMouseButtonDown (0)) {
Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(Ray, out RaycastHit)) {
Destroy(RaycastHit.collider.gameObject);
}
}
}发布于 2017-02-26 17:27:34
是的另一种方式:
using UnityEngine;
using System.Collections;
public class CatDestructor : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnMouseDown()
{
// Destroy game object
Destroy (this.gameObject);
}
}把这个脚本放在“猫”预制件上,如果你点击它,它会破坏“猫”。
或您必须将代码放入更新函数,如下所示:
void Update(){
if (Input.GetMouseButtonDown(0)){ // if left button pressed...
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)){
// the object identified by hit.transform was clicked
// do whatever you want
}
}
}发布于 2017-02-21 20:43:03
您不应该签入更新函数吗?
发布于 2017-02-26 17:09:00
就像Arne说的,确保在更新函数中检查它,如果它是2d对撞机,请确保将它更改为
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);
if (hit.collider != null)
{
// do whatever you want to do here
}
}https://stackoverflow.com/questions/42376838
复制相似问题