Input.GetkeyDown 在没有按钮按下的情况下返回 true

我正在制作一个系统,您可以在其中按下某个键,它将在您的鼠标位置放置一个对象。每次按下“1”键时,它都应该将其放在鼠标位置,但由于某种原因,每一帧都Input.GetKeyDown("Alpha1");被注册为 true,因此无论我移动鼠标,无论我按什么,它都会将块向下放置。最近经常发生这种情况,我似乎找不到任何答案。


using UnityEngine;


public class CubePlacer : MonoBehaviour

{

    private Grid grid;

    public KeyCode place;


    private void Awake()

    {

        grid = FindObjectOfType<Grid>();

    }


    private void Update()

    {

        if (Input.GetKeyDown(place)) ;

        {

            RaycastHit hitInfo;

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);


            if (Physics.Raycast(ray, out hitInfo))

            {

                PlaceCubeNear(hitInfo.point);

            }

        }

    }


    private void PlaceCubeNear(Vector3 clickPoint)

    {

        var finalPosition = grid.GetNearestPointOnGrid(clickPoint);

        GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = finalPosition;


        //GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = nearPoint;

    }

}


牛魔王的故事
浏览 116回答 1
1回答

拉莫斯之舞

if (Input.GetKeyDown(place)) ; // <--- remove this semi colon&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RaycastHit hitInfo;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Physics.Raycast(ray, out hitInfo))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlaceCubeNear(hitInfo.point);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }您需要删除 If 语句后的分号。每次Update()调用该方法时,它都会终止该行并导致块随后执行。
打开App,查看更多内容
随时随地看视频慕课网APP