当我单击鼠标右键时,我要创建新的球体。而且我不知道为什么这行不通。它创建一个球体,但绝对不在鼠标位置!
Vector2 mousePos;
public Transform mousePointer;
float mouseX, mouseY;
Vector3 spawnPoint;
void Start () {
}
void Update () {
if(Input.GetMouseButtonDown(1)){
mousePos = Input.mousePosition;
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
spawnPoint = new Vector3(mouseX, mouseY, 0);
Instantiate(mousePointer, spawnPoint, Quaternion.identity);
}
}
最佳答案
尝试相对于相机生成对象。
例如,使用spawnPoint = cameraPosition + new Vector3(mouseX, mouseY, 0);
或类似的名称。查看相关文章:Create a cube relative to camera mouse position。
正在以全局坐标生成对象。