Hi guys, i am working on a Sub-System, a road creation in a simCity way. i am in a phase of experimentation, i know the basics, so here is my question.
i am using a function named mousePosition,with raycasting. i create a mesh dinamicaly(4 vertices), The idea is simple, i made 1 click and CreateRoad is called. The center of the mesh is exactly where the mouse was clicked. the road lenght is 4.
Alpha(4Pix)-------Beta(0pix)
imagine the road with both extremes Alpha and Beta. the center is on B.
i want to rotate Alpha toward the mouse location. point Beta must be fixed and A always have to be looking at mouse location. My problem is that my road rotates with Beta looking the mouse location, not Alpha.The road rotates complete, not with a fixed point. i have tried to add a GameEmpty like a parent but i cant separate the Transform from the road mesh becouse there is no option to do this from Unity. i have tried a lot of codes and i dont get it to work, i have readed a lot of post too. here is the complete script with comments.
i would love to know if somebody knows how to do it correctly. Thanks a lot, and hope you understand my question. Image beneath.
using UnityEngine;
using System.Collections;
public class GroundClick : MonoBehaviour {
public GameObject prefabRoad;
public GameObject nodeRotation;
public GameObject prefabNode;
float lenght;
Vector3 roadEnd;
Vector3 roadStart;
Vector3 lookAtRoad;
float width;
Mesh mesh;
bool boton;
GameObject road;
RaycastHit hitinfo ;
Ray ray;
MeshFilter mesh_filter;
int creandoCalle; //si vale 1 se dio un click y se esta posicionando la calle
// si vale 2 se dio el segundo click y se lo pone definitivamente
//si vale cero no se hizo click sobre crear calle
void Start(){
creandoCalle=0;
}
void Update () {
if(Input.GetMouseButtonDown(0))
{
getClickLocation(out roadStart);
createRoad(roadStart);
creandoCalle=1;
}
if(creandoCalle==1){
rotate();
}
}
void rotate()
{
getClickLocation(out lookAtRoad); //obtain the mouse position and saves it on LookAtRoad
Debug.Log(lookAtRoad);
//Quaternion rot=Quaternion.LookRotation(lookAtRoad);
//rot*= Quaternion.Euler(new Vector3(0,0,0));
//road.transform.rotation=Quaternion.Slerp(road.transform.rotation,rot,Time.deltaTime*100);
//road.transform.rotation= Quaternion.RotateTowards(road.transform.rotation,rot,1);
//road.transform.rotation=Quaternion.FromToRotation(road.transform.position,lookAtRoad.normalized);
//road.transform.RotateAround(road.transform.forward*new Vector3(0,0,4),new Vector3(0,0,1),Quaternion.Angle(road.transform.rotation,))
road.transform.LookAt(lookAtRoad);
//road.transform.rotation=Quaternion.FromToRotation(road.transform.position,lookAtRoad);
//road.transform.Rotate
}
void TerminarTramoDeCalle ()
{
}
bool getClickLocation(out Vector3 point)
{
ray=Camera.main.ScreenPointToRay(Input.mousePosition);
hitinfo =new RaycastHit();
if(collider.Raycast(ray,out hitinfo, Mathf.Infinity)){
point =hitinfo.point;
return true;
}
point= Vector3.zero;
return false;
}
void createRoad(Vector3 roadStart){
road= Instantiate(prefabRoad) as GameObject;
road.transform.position = roadStart+ new Vector3(0,0.01f,0);
//road.transform.rotation= Quaternion.FromToRotation(roadStart, roadEnd);
width=1;
lenght=4;
Vector3[] vertices = new Vector3[]
{
new Vector3( 1, 0, 1),
new Vector3( 1, 0, -5),
new Vector3(-1, 0, 1),
new Vector3(-1, 0, -5),
};
int[] triangles = new int[]
{
0, 1, 2,
2, 1, 3,
};
Vector2[] uv = new Vector2[]
{
new Vector2(1, 1),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(0, 0),
};
Vector3[] normals={
Vector3.up,
Vector3.up,
Vector3.up,
Vector3.up
};
mesh= new Mesh();
mesh.vertices=vertices;
mesh.triangles=triangles;
mesh.uv=uv;
mesh.normals=normals;
mesh_filter=road.GetComponent<MeshFilter>();
mesh_filter.mesh=mesh;
}
}
i have tried a lots of code that doesnt work properly.