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!