我在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);
}
}
答案 0 :(得分:1)
您需要计算ScreenToWorld坐标。使用Unity它实际上很容易。
https://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting