每當我單擊時,我都試圖讓一個物件在 unity 2d 中的滑鼠位置生成,但沒有顯示任何物件。它在層次結構中添加了新的克隆,但只是不顯示。
這是游戲控制器物件的腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gamecon : MonoBehaviour
{
public GameObject square;
public void Start()
{
}
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 spawnpos = Camera.main.WorldToScreenPoint(Input.mousePosition);
GameObject g =Instantiate(square, (Vector2)spawnpos, Quaternion.identity);
}
}
}
我無法在網上找到適用于我的情況的任何答案。感謝幫助。
uj5u.com熱心網友回復:
解決方案是使用ScreenToWorldPoint方法而不是WorldToScreenPoint,因為您需要的是生成物件的世界位置。
使用以下代碼:
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 spawnpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
GameObject g =Instantiate(square, (Vector2)spawnpos, Quaternion.identity);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/536466.html
標籤:C#unity3d