x


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);

}

more ▼

asked Oct 05 '10 at 02:20 PM

Jeff 5 gravatar image

Jeff 5
17 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Oct 05 '10 at 03:05 PM

Loius gravatar image

Loius
10.9k 1 12 43

Agreed, I would probably just use LookAt() in this situation.

Jan 06 '11 at 03:42 PM PrimeDerektive
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x320
x300
x296
x142
x132

asked: Oct 05 '10 at 02:20 PM

Seen: 1512 times

Last Updated: Oct 05 '10 at 02:20 PM