- Home /
Question by
kaliban · Jun 02, 2013 at 03:33 PM ·
cameramousepositionplane
MousePosition input problem after rotating object.
I have this script:
public Camera MainCamera;
void Update()
{
towerRotation();
}
void towerRotation()
{
Vector3 frontofplane;
Vector3 ff2p;
frontofplane=transform.localPosition+ transform.forward;
ff2p=frontofplane + transform.right*2;
Plane playerPlane = new Plane(frontofplane,ff2p);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.localRotation = Quaternion.Slerp(transform.rotation, targetRotation, 5 * Time.time);
}
It works well if the object(to which one this script is attached) is not rotated.
But if I rotate that object - this script goes crazy.
If I rotate object for 180, mousePosition input is inversed :(
How to make this script rotation independant?
I see an opportunity to make static plane, which would rotate with main object, but I dont know how to make it yet...
Comment