I want to create a Script, which spawns the GameObject “MainObject” at the position, where my Ray hits the GameObject with the Tag “Wall”. How can I do this?
At the moment I have this script:
public string tag = "Wall";
public GameObject MainObject;
void Update()
{
if (Input.touchCount == 1)
{
Touch touched = Input.GetTouch(0);
RaycastHit hitted;
Ray ray = Camera.main.ScreenPointToRay(touched.position);
if (Physics.Raycast(ray, out hitted, distance))
{
if (hitted.collider.CompareTag(tag))
{
Vector3 create = new Vector3(touched.position.x,touched.position.y,0);
Instantiate(MainObject, create, Quaternion.identity);
}
}
}
}