0

我正在用 Unity 编写游戏。每次用户单击界面时,我都试图在空间中生成一个球体。我可以生成球体,但球体始终位于 (200,200, 0) 和 (400,400,0) 左右的坐标处,但球体应该在屏幕上指针所在的位置生成。下面是我的代码,有人可以帮忙吗?我正在用 C# 编写:

using UnityEngine;
using System.Collections;

public class myscript : MonoBehaviour {

//initialize a circle
public GameObject Node;
public float cooldown = 1;

bool clicker;
float clicktime = 0;

GameObject node; //reference to the prefab

// Use this for initialization
void Start () {
}

//In everyframe you can 
void Update () {
    clicker = Input.GetMouseButtonDown(0); //obtaining the input
    clicktime += Time.deltaTime;

    }

//Check whether you can shoot
void FixedUpdate(){
    if (clicker == true && clicktime >= cooldown) { //if the clicker is clicked and the previos clicking is done
        SpawnCircle(); //spawn a circle
        clicktime = 0; //reset timer
    }
}

void SpawnCircle(){
    //this creates a new game object
    x = Input.mousePosition.x
    y = Input.mousePosition.
    node = GameObject.Instantiate (Node,Input.mousePosition,Quaternion.identity) as GameObject;
    //settings that we initalize our object with
    //node.transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
}

}
4

1 回答 1

1

您需要计算 ScreenToWorld 坐标。使用 Unity 实际上很容易。

https://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting

于 2015-10-24T21:45:09.097 回答