- Home /
Rotate to mouse seems to be inversed
This picture helps show my current setup -
The pole's pivot point is where the sphere is, and I want the pole to rotate to the mouse position when the mouse is over the plane. This is the code -
public var turnSpeed:Number = 1000;
private var lookTarget:Vector3;
function Update()
{
var ray:Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit:RaycastHit;
if (Physics.Raycast(ray, hit))
{
if (hit.collider.tag == "myPlane")
{
lookTarget = hit.point;
}
}
// rotate towards target
var targetRotation:Quaternion = Quaternion.LookRotation(lookTarget);
var rotationSpeed:Number = turnSpeed * Time.deltaTime;
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed);
}
The raycasting part works - the pole only moves when the mouse is over the plane.
The problem is when I point to the top right of the plane then the pole points to the bottom left and visa versa... so basically all the axis seem inversed? Also if the mouse is in the centre of the plain the pole tries to point to the sky but only gets about halfway.
Any help on this would be appreciated!
Answer by turtlelizer · Jan 21, 2012 at 11:34 AM
the raycast can only see u when u are down on the plane, because Physics.Raycast are looking for a collider. So what i didnt to fix it, was making a plane behind the object, there is as big as the screen.
your coding looks fine, even though im a new and a C# programmer (:
try it, maybe helps
Peace mate (: