I have seen a lot of examples using a boolean to only instantiate once in update and I have followed the examples but it’s not working for me. I’m probably missing something here, please help me find it.
using UnityEngine;
using System.Collections;
public class FurnitureManagerScript : MonoBehaviour
{
public GameObject gameobjecttoplace;
public RaycastHit hit;
public bool click;
// Use this for initialization
void Start()
{
click = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0) && click==false && gameobjecttoplace!=null)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
click = true;
spawnatclick();
}
}
void spawnatclick()
{
if (click == true && hit.transform.tag == "BuildableArea")
{
click = false;
Instantiate(gameobjecttoplace, Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity);
Debug.Log(click);
}
}
}
I am trying to let the player select a button which stores what gameobject the player wants to place in an area and then instantiate it once. However, right now it is spawning based on frame rate so I’m getting like a lot of gameobjects.
Use
Input.GetMouseButtonDown(0) //You only want 1 click!
Use
hit.point //Spawn at the Raycasted position instead of at the camera
using UnityEngine;
using System.Collections;
public class FurnitureManagerScript : MonoBehaviour
{
public GameObject gameobjecttoplace;
public RaycastHit hit;
public bool click;
void Start()
{
click = false;
}
void Update()
{
if (Input.GetMouseButtonDown(0) && click==false && gameobjecttoplace!=null)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
click = true;
spawnatclick();
}
}
void spawnatclick()
{
if (click == true && hit.transform.tag == "BuildableArea")
{
click = false;
Instantiate(gameobjecttoplace, hit.point, Quaternion.identity);
}
}
}