Quantcast
Channel: Answers for "Shooting problem - bullet shoots diagonally when facing forward"
Viewing all articles
Browse latest Browse all 4

Answer by Cyb3rManiak

0
0

Vector3.forward will always return a Vector3 of (0,0,1). This is the forward direction relative to the world coordinate system. What you need is the relative forward of the bullet. Try this...

var instanceBullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
var v3RelativeForward: Vector3 = instanceBullet.transform.TransformDirection(Vector3.forward);
instanceBullet.rigidbody.AddForce(v3RelativeForward * shootForce)AddForce(v3RelativeForward * shootForce);

EDIT: When going over OrangeLightning's comment again, It just stuck me that you're also instantiating the bullet using Quaternion.identity, and just like he said, this means that the bullet's forward will also always be (0,0,1) all the time. I've edited your Instantiate() to use transform.rotation when instantiating the bullet. This will create the bullet aligned with whatever "transform" is pointing to. So if this is the character - the bullet will face to the character's forward direction as well, and the TransformDirection() will make sure that the force you add to it will be in that direction.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images