If you want some object to stay looking at the mouse cursor, you must calculate the world point corresponding to the mouse position. The function Camera.ScreenToWorldPoint can do this: copy mousePosition to some Vector3 variable and set its Z coordinate to the distance between the point and the camera, then pass this variable to ScreenToWorldPoint (a point in the screen becomes a line in the 3D world, thus you must define at which distance from the camera the desired point is).
The script below makes its owner to keep looking at the cursor. The distance chosen is half the distance between the camera and the object - not mathematically correct, but gives very acceptable results:
function Update () {
var mousePos = Input.mousePosition;
// get the mid-point distance between camera and object
mousePos.z = Vector3.Distance(Camera.main.transform.position, transform.position)/2;
// look at this world point
transform.LookAt(Camera.main.ScreenToWorldPoint(mousePos));
}
I don’t know if this is exactly what you’re trying to do - your code looks more like a “shoot where I’m clicking” thing, while this script is a “look at the cursor” code (like the Avert Fate initial screen).
EDITED: Well, the code above is interesting to keep some object in the 3D world looking at the mouse pointer, but is absolutely useless in your case, since you’re trying to shoot a bullet in the direction defined by the mouse pointer in a 2D game.
From your last comment, I deduced that the main problem here is the use of LookRotation to align the bullet to the target: LookRotation points the Z axis to the target, but your bullet’s front direction seems to be X. You could fix this by using FromToRotation(Vector3.right, rotation) instead - the rotation returned would align the X axis (Vector3.right) to the mouse pointer direction.
By the way, the direction you’re calculating is the mouse position relative to the screen center. This will only work if the shooter is exactly at the center, and the scene has zero rotation. If you want a more precise and robust way to find the mouse direction, use this approach: create a temporary horizontal plane (the geometric entity, not the mesh) at the shooter position and use ScreenPointToRay, Plane.Raycast and Ray.GetPoint to find the position where the mouse pointer is on the plane, then calculate the direction shooter->mouse position:
...
if (canRotate == true) {
// create a horizontal plane at the shooter position:
var shotPlane = new Plane(Vector3.up, myTransform.position);
// create a ray from the mouse pointer:
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// cast the ray against the plane:
var dist: float = 0;
if (shotPlane.Raycast(ray, dist)){
// get the point hit in the plane:
var clickPoint = ray.GetPoint(dist);
// find the direction to rotate:
rotation = clickPoint - myTransform.position;
}
}
...
function PlayerAttackUpdate () {
// define the position (in a slightly faster way):
var pos = myTransform.position; pos.y = 1.1;
// find the rotation "X axis -> rotation direction"
var rot = Quaternion.FromToRotation(Vector3.right, rotation);
// fire!
aBullet = Instantiate(bullet, pos, rot);
}