1) Игрок нажимает на кнопку "Постройка"
2) Появляется "призрак" постройки который двигается за курсором
3) По второму нажатию кнопки мыши постройка ставится на карте
Проблема в том что по нажатии на кнопку "Постройка", создаётся объект из префаба сразу на том месте где он находился при удалении из иерархии, вообщем на своих стандартных координатах. Ни как не могу понять в чём проблема. Буду благодарен за помощь. Заранее спасибо
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuildMenu : MonoBehaviour {
public int width = 200;
public int height = 35;
public GameObject prefab;
GameObject instance;
void Update() {
if (instance != null)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.name == "Ground")
{
instance.transform.position = hit.point;
}
}
if (Input.GetMouseButton(0))
{
instance = null;
}
}
}
void OnGUI() {
GUILayout.BeginArea(new Rect(Screen.width/2 - width/2,
Screen.height - height,
width,
height), "", "box");
GUI.enabled = (instance == null);
if (GUILayout.Button("Постройка"))
{
instance = (GameObject)GameObject.Instantiate(prefab);
}
GUILayout.EndArea();
}
}
using System.Collections.Generic;
using UnityEngine;
public class BuildMenu : MonoBehaviour {
public int width = 200;
public int height = 35;
public GameObject prefab;
GameObject instance;
void Update() {
if (instance != null)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.name == "Ground")
{
instance.transform.position = hit.point;
}
}
if (Input.GetMouseButton(0))
{
instance = null;
}
}
}
void OnGUI() {
GUILayout.BeginArea(new Rect(Screen.width/2 - width/2,
Screen.height - height,
width,
height), "", "box");
GUI.enabled = (instance == null);
if (GUILayout.Button("Постройка"))
{
instance = (GameObject)GameObject.Instantiate(prefab);
}
GUILayout.EndArea();
}
}