- Home /
The question is answered, right answer was accepted
Rigidbody2D.AddRelativeForce not working
I'm making an action platformer. I'm having the player shoot a shot gun with a spread shot like this (excuse the lack of drawing skills I can't draw): I'm using Rigidbody2D.AddRelativeForce
But for some reason the bullets don't move forward, they spawn but just sit there. Here's a picture of the Rigidbody on each bullet. I do want to be adding local force, so they can seperate
And finally here's the relevant code from the player script:
void ShootWeapon()
{
for (float j = -bulletSpreadAngle; j < bulletSpreadAngle; j += bulletSpreadAngle / numOfBullets * 2)
{
GameObject bullet = Instantiate(bulletPrefab, gunEnd.position, Quaternion.identity);
Rigidbody2D projectileRigidbody = bullet.GetComponent<Rigidbody2D>();
print(j);
bullet.transform.localRotation = new Quaternion(Quaternion.identity.x, Quaternion.identity.y, j, Quaternion.identity.w);
projectileRigidbody.AddRelativeForce(new Vector2(bulletSpeed, projectileRigidbody.velocity.y), ForceMode2D.Impulse);
}
}
Answer by NoobGaming · Jan 22, 2018 at 06:27 PM
I'm guess that the "Body Type Kinematic" might be your problem? Being kinematic means the rigidbody doesn't get forces applied to it. Try changing that up. Hope this helps!
Yes, changing the Rigidbody to dynamic and setting the gravity scale fixed the problem. However, they don't move along the local axis as I had hoped. any help on that?
Awesome! We'll it depends how you have you gameobject positioned. You have projectileRigidbody.velocity.y which is the up axis. If you want it to fly forward then do projectileRigidbody.velocity.x
Hope that helps!
They go along the global x axis but not on the local x axis