Hello, I am having trouble making a prefab follow the mouse cursor before placing it in my scene in the game. I currently have been following some of Brackeys tower defense tutorial for the parts I needed to learn and I got my prefab to spawn in the game after clicking a UI button and then clicking the mouse. I am not making a game with tiles or “nodes” as he is calling them, but instead I wanted the option to spawn this prefab wherever the player chooses in the scene. I have spent about 10 hours just trying to search the internet for something to fit my needs, but all of the stuff I found has either been outdated or I am unable to implement it into my code. I am very new to this, and still trying to make sense of what all the code means, but I am starting to get it down. I have tried many things on my own and from the unity docs on instantiating, but I cannot seem to grasp this concept well enough. If someone could help point me in the right direction or provide some code to get me started, I would really appreciate it! I am also trying to figure out how to rotate the object before placing it if you can start me on that as well.
Currently I can click on the UI button and then click somewhere in the level to place the object. This works good, but it does allow spawning through other objects and on top of them. That’s another problem I will be working on. My main concerns are:
- Creating a “preview” of the object that will follow the users mouse until the button is clicked again to place it in the level
-Implementing a key press to allow for rotation of the object before being placed
Again, any help would be appreciated. Sorry If my code is atrocious. ![]()
using UnityEngine;
public class BuildManager : MonoBehaviour {
public static BuildManager instance;
void Awake ()
{
if (instance != null)
{
Debug.Log("More than one BuildManager in Scene!");
return;
}
instance = this;
}
public GameObject woodenFencePrefab;
private GameObject defenseToBuild;
public GameObject GetDefenseToBuild ()
{
return defenseToBuild;
}
public void SetDefenseToBuild (GameObject defense)
{
defenseToBuild = defense;
}
}
using UnityEngine;
public class RaycastBuilder : MonoBehaviour {
public GameObject defense;
public Vector3 positionOffset;
public bool IsPurchased = false;
Vector3 mousePosition;
BuildManager buildManager;
void Start ()
{
buildManager = BuildManager.instance;
}
void Update ()
{
GameObject defenseToBuild = buildManager.GetDefenseToBuild();
//To get the current mouse position
mousePosition = Input.mousePosition;
if (Input.GetMouseButton(0)&& IsPurchased == true)
{
Ray ray = Camera.main.ScreenPointToRay(mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
defense = (GameObject)Instantiate(defenseToBuild, hit.point + positionOffset, Quaternion.identity);
IsPurchased = false;
}
}
}
}
using UnityEngine;
public class Shop : MonoBehaviour {
BuildManager buildManager;
void Start ()
{
buildManager = BuildManager.instance;
}
public void PurchaseWoodenFence ()
{
Camera.main.GetComponent<RaycastBuilder>().IsPurchased = true;
Debug.Log("Wooden Fence Purchased");
buildManager.SetDefenseToBuild(buildManager.woodenFencePrefab);
}
}
Thanks for taking the time out of your day to help me!
EDIT: bigmisterb has fixed my issue for me, can’t thank him enough!
3160706–240452–BuildManager.cs (586 Bytes)
3160706–240453–RaycastBuilder.cs (938 Bytes)
3160706–240454–Shop.cs (419 Bytes)