【问题标题】:Spawning in Unity: Spheres flying out of range在 Unity 中生成:球体飞出范围
【发布时间】:2015-10-24 21:41:23
【问题描述】:

我正在使用 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);
}

}

【问题讨论】:

    标签: c# unity3d mouseclick-event spawning


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 2019-04-15
      • 2020-12-23
      相关资源
      最近更新 更多