- Home /
transform.lookat wrong direction
hello im instantiating an object that is kinematic by default, however i want it to look at my mouse position before i launch it (in a seperate script i think will make those to one when everything works) currently i click behind the object and it lauchnes in the opposit direction i want it to fly right to my mouse position here is my script i cant find the mistake there
if (Input.GetMouseButtonDown(0))
{
Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if (groundPlane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLength);
Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
}
}
in addition i would like to replace GetMouseButtonDown with something like WhileMouseButtonDown but i cant find the function on google so that would be a helpful addition
thanks for help :)
Answer by bhavinbhai2707 · Apr 25, 2018 at 07:42 PM
Since it faces in opposite direction then it will be easy to just give the direction towards target!! try using transform.LookAt(2 * transform.position - target.transform.position);
where target is your mouse position in world space!!
hey thanks for the reply! i´ve tried that already but it didnt work for me. but now i noticed all my models made with blender have weird rotations and stuff so i assume thats the problem here? didnt get to try fixing it that way yet error.404 time not found :D
Rotate the model pivot point by 180 degrees in Blender m8
Answer by RLin · Apr 25, 2018 at 11:21 PM
Try using transform.forward = transform.position - target.transform.position;
thanks for the reply, my problem was my blender model i just exported it without setting its rotations to suit unity