簡體   English   中英

C#unity使用射線投射命中信息更改變量

[英]C# unity Changing a variable using ray cast hit information

此游戲中的相機的目標可以通過單擊其他模型來更改,然后將成為相機的焦點,下面的腳本是我到目前為止所獲得的內容,但是每次我單擊游戲中的對象時,目標都不會顯示任何內容而不是任何模型。

Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool didHit = Physics.Raycast(toMouse, out hitInfo);

if (didHit)
{
    if (hitInfo.collider.tag == "Cell" && Input.GetMouseButtonDown(0))
    {
        Debug.Log("Cell hit");

        target = hitInfo.transform.Find(gameObject.name);       
    }
}

如果此腳本在相機上,則應執行以下操作:

GameObject target;
// or
Transform target;

void Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(ray, out hit))
        {
            target = hit.transform.gameObject;
            // or
            target = hit.transform;
        }
    }   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM