- Home /
rotating road gameobject towards mouse position
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);
}
}
}
Answer by robertbu · Dec 29, 2013 at 02:48 AM
It appears this is a top-down view and you are scaling the objects on the 'z' axis? If so, you can calculate the rotation like:
Road.transform.rotation = Quaternion.FromToRotation(Vector3.forward, mousePos2 - mousePos1);
oh sorry i forgot to mention that i can rotate my camera 360 degrees as well as move it about with the wasd keys in pretty much any direction. Im basically trying to rotate the gameobject around its y axis towards the mouse cursor. Its basically going to be a city building type of game.
If the camera can rotate, then you need to convert mousePos1 and mousePos2 into world coordinates before you calculate the rotation. If the camera is parallel to the XZ plane, you can use Camera.ScreenToWorldPoint(). If the camera is at an angle then some form of raycast will work. I recommend using Unity's mathematical Plane class and using Plane.Raycast() to find the world points. The calculation for the rotation will be as I've indicated only using world positions.
Finally figured it out with your help thanks so much! I created a new plane and a ray which emitted from the mouse position and used the following if statement code for the rotation. I used quaternion.slerp as it give me a smooth rotation.
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);
}