- Home /
nee help with Instantiating prefab at mouse position with GUI button
hi, I finally got my code working that lets me so far lengthen a piece of road prefab thats already in the scene to any length i want and also rotate it by clicking on it and dragging it. My camera is a overhead camera that i can rotate and move around. What I need help with is i need to instantiate the road prefab by clicking on a GUI button then click and drag it to the length i want and place it at that length on the terrain (kind of like cities xl or simcity).
Problem is that none of the methods i tried seem to work and i also cant find anything on google that would help me. here is the code.
using UnityEngine;
using System.Collections;
public class Roadplacement : MonoBehaviour
{
public GameObject Road;
public GameObject RoadPrefab;
private Ray ray;
private float distObjRay;
private float RoadLength;
private float RoadScale_z;
private float hitdist = 0.0f;
private float speed = 30.0f;
private Vector3 objectPos;
private Vector3 Raypoint;
private Vector3 Raypointxz;
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButton(0))
{
// Initial scale and position vector of road
Road.transform.localScale = new Vector3(Road.transform.localScale.x,Road.transform.localScale.y,0f);
//Ray is being cast at mouse Position
Plane roadPlane = new Plane(Vector3.up, transform.position);
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// World Coordinates of Ray
Vector3 Raypoint = ray.GetPoint(hitdist);
Vector3 Raypointxz = new Vector3(Raypoint.x,0,Raypoint.z);
Vector3 objectPos = new Vector3(Road.transform.position.x,0,Road.transform.position.z);
//Distance of object to Ray
float distObjRay = Vector3.Distance(Raypointxz,objectPos);
//Rotation of Road object towards mouse position
if (roadPlane.Raycast (ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
// New Scale based on the distance between mouse position and the object
RoadScale_z = distObjRay *0.125f;
// Applies the new z scale
Road.transform.localScale = new Vector3(Road.transform.localScale.x,Road.transform.localScale.y,-RoadScale_z);
// Tiles the road texture according to new scale of the prefab
RoadPrefab.renderer.material.mainTextureScale = new Vector2(1,Road.transform.localScale.z *2);
}
}
}
I think it needs to instantiated on input.getmousebuttonup
after i dragged the road prefab with input.getmousebutton
. I also think i need to save the inital mouse position on the ground which would be the starting point for the road. I dont know how to implement all that to get it to work. The code i have so far works fine with one road prefab in the seen. Maybe someone can help understand how instantiating actually works. Thanks.
I figured it out it wouldnt instantiate because the road prefab had an an empty gameobject as its parent and i didnt realize that it actually tried to instantiate the empty gameobjet.