- Home /
Setting rotation angle of a bullet fired towards the mouse
Hi all,
I am creating a top down 2D shmup and have successfully instatiated a bullet prefab.
During the Start Handler the bullet converts the 2d mouse coordinates into 3d World coordinates. The difference current position (player) and mouse is normalised to create a vector that i use in the Update handler.
The trouble is the bullet plane and texture which is rectangular needs to be rotated toward the in the angle of this vector. I am using eularAngles to rotate the prefab
Ive even tried hardcoding an angle in but don't seem to be getting a result:-
transform.eulerAngles = new Vector3(0, 0, 20);
Here is the scipt:-
function Start(){
var targetScreenPos = Input.mousePosition; targetScreenPos.z = distFromCam; var mouseTargetPostion = Camera.main.ScreenToWorldPoint(targetScreenPos);
var targetDelta = mouseTargetPostion - transform.position; launchVelocity = targetDelta.normalized * bulletSpeed;
//var targetAngle = Quaternion.LookRotation(mouseTargetPostion);
transform.eulerAngles = Vector3.Angle(targetDelta, transform.right);
}
Answer by Loius · Oct 05, 2010 at 03:05 PM
Have you tried Transform.LookAt? You may need to make the bullet a child of something if the bullet model is rotated by default such that its Z axis is not forward.
Agreed, I would probably just use LookAt() in this situation.