Hello,
i try to create a little game, and i´m a little bit confused.
I build up a Grid system, so you can build where ever you want. Also no problem…
But if i try to build a prefab object with a mouse click event, it doesn´t spawn, the object at the click point, i hope someone can help me.
using System;
using UnityEngine;
public class WallPlacer : MonoBehaviour {
private Grid grid;
RaycastHit hit;
public GameObject prefab;
private void Awake ()
{
grid = FindObjectOfType<Grid>();
}
private void Update ()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitInfo))
{
PlaceWallNear(hitInfo.point);
}
}
}
private void PlaceWallNear(Vector3 clickPoint)
{
var finalPosition = grid.GetNearestPointOnGrid(clickPoint);
GameObject obj = Instantiate(prefab, prefab.transform.position, prefab.transform.rotation) as GameObject;
}
}