- Home /
How do I add rotation to movement so that a bullet does not travel in a straight line?
I know this is one of those questions most likely asked a dozen times, but I'm having some serious trouble with this. I'm working on a 2D sidescroller, and my character can lean back and forth to shoot a gun. However, the particle bullet only travels in a singular straight direction, ignoring all attempts to add the starting rotation to it's direction of travel. I want the particle to travel in the direction the gun is aimed towards, which is based on the spine of the character.
Transform bullet = Instantiate(particleFire, launchLocation.position, bone.transform.localRotation) as Transform;
bullet.LookAt(bone.transform.localRotation.eulerAngles);
bullet.rigidbody.velocity = Vector3.right * 8;
LookAt takes a position in world space, not an angle. How you solve this problem will depend on your models and what you define as the 'forward' in your 2D environment. Are they all sprites, or is part/all of them 3D models? If the 'bone' has a rotation of (0,0,0), what direction defines 'forward'?
Answer by supernat · Feb 03, 2014 at 06:06 AM
Parent the emitter to the gun if you're using an emitter, which I can't tell from the code but doesn't look like you're using a particle system here. If you're just instantiating an object, the direction it travels will be determined by the script driving it or the physics.
LookAt takes a position, not a rotation. Eulerangles are your rotation values. Also, it appears you were attempting to tell the bullet to look at the bone. Usually you use LookAt when you want to adjust the eulerAngles of a transform such that the object is facing he target. So I don't think you want it to look at the bone.
It seems you are relying on the physics to move the bullet, so by telling the bullet to move with a velocity to the right, it will move in a straight line along the right axis (+X) as you are in world coordinates, not body. I think you can use rigidbody.AddRelativeForce(Vector3.right*8) or use Vector3.forward depending on your setup. You would need to parent the bullet to the gun (bullet.transform.parent = gun.transform) first. Then the AddRelativeForce should work. You might have to tweak it if the gun transform is not facing down the barel in body cords.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132408im_/https://answers.unity.com/themes/thub/images/avi.jpg)