So i made this code here which lets me lengthen a plane with a road texture on it to any length i want (and still maintain proper texture tiling) based on distance between two mouse positions. What I’m trying to do now is to rotate my road (around its pivot basically which i set up to be on one end point of the road object) towards wherever the mouse position or mouse coordinates are I would like it to be similar to the way you place roads in simcity where you click on the ground and rotate the road in any direction you want. I just cant seem to figure out how to do it nothing i tried worked so far. Maybe someone can help me out. I hope i explained it clearly enough.
public class Roadplacement : MonoBehaviour
{
public GameObject Road;
public GameObject RoadPrefab;
private Vector3 mousePos1;
private Vector3 mousePos2;
private float RoadLength;
private float RoadScale;
// Use this for initialization
void Start ()
{
}
void OnGUI()
{
}
// Update is called once per frame
void Update ()
{
Vector3 m = Input.mousePosition;
m = new Vector3(m.x,m.y,transform.position.y);
Vector3 p = Camera.main.ScreenToWorldPoint(m);
Vector3 mousecoord = new Vector3(p.x,0,p.z);
//Road.transform.eulerAngles = new Vector3(0,Road.transform.eulerAngles.y,0);
//Debug.Log(Road.transform.eulerAngles);
if(Input.GetMouseButtonDown(0))
{
mousePos1 = new Vector3(p.x,0,p.z);
//Debug.Log(mousePos1);
}
if(Input.GetMouseButton(0))
{
mousePos2 = new Vector3(p.x,0,p.z);
Vector3 offset = (new Vector3(0,0,mousePos2.z)) - (new Vector3(0,0,mousePos1.z));
RoadLength = offset.magnitude;
RoadScale = RoadLength * 0.1f;
if(offset.z > 0)
{
Road.transform.localScale = new Vector3(Road.transform.localScale.x,Road.transform.localScale.y,RoadScale);
}
if(offset.z < 0)
{
Road.transform.localScale = new Vector3(Road.transform.localScale.x,Road.transform.localScale.y,-RoadScale);
}
RoadPrefab.renderer.material.mainTextureScale = new Vector2(1,Road.transform.localScale.z *2);
Debug.Log(offset);
Debug.Log(RoadLength);
Debug.Log(RoadScale);
Debug.Log(mousePos2);
Debug.Log(Road.transform.localScale);
}
}
}