So I have a bullet script that fires a bullet straight in the Y direction that gets called as the bullet is instantiated. The plan is to always shoot in the same direction, straight in the Y, but to just rotate the bullet upon instantiation so that the straight is the right kind of straight if that makes sense? What i really want to know i think is how to work out the value for the 3rd property of the instantiate based on the key pressed before the fire, what makes it worse is that i dont even know how to create a rotation variable.

Instantiate(Resources.Load("P1Bullet"), transform.position, transform.rotation);

this is how i move my bullet in the bulletmove script

void Update () {
    Vector3 pos = transform.position;
    Vector3 vel = new Vector3(0, speed * Time.deltaTime, 0);
    pos = pos + transform.rotation * vel;
    transform.position = pos ;
  • \$\begingroup\$ rotate the bullet how? so that it points in the Y direction? \$\endgroup\$ Apr 4 '16 at 20:30
  • \$\begingroup\$ so that it faces in the direction of the player \$\endgroup\$ Apr 4 '16 at 21:08

What you need is a quaternion. The rotation variable that you mentioned.

To correctly create a quaternion in the direction you want you need to create it using the Euler method. To create your object:

Instantiate(Resources.Load("P1Bullet"), transform.position, Quaternion.Euler(-90f, 0f, 0f));

As I don't know the direction you want to face and the orientation of your model you might have to change the values a bit.

To make the bullet always look at the player (no matter were he is) use this:

Instantiate(Resources.Load("P1Bullet"), transform.position, Quaternion.LoopRotation(player.transform.position - transform.position, Vector3.up));
  • \$\begingroup\$ Thanks, I'll try it out, what I essentially want is to rotate the bullet in the direction the player is facing, so if "D" is pressed it should go to the right \$\endgroup\$ Apr 4 '16 at 21:10
  • \$\begingroup\$ Just make a switch case for each button and instantiate them in a different way with different roatations \$\endgroup\$ Apr 4 '16 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.