- Home /
Question by
LeftyTwoGuns · Jul 26, 2017 at 01:18 AM ·
physicsrigidbodyvelocityprojectilemagnitude
Adding player velocity to projectile not working
I'm using game objects as projectiles and I can't get them to instantiate with the velocity of the moving player. The projectiles will instantiate a distance away from the gun while the player is moving, instead of on the gun barrel where they are instantiated. Here's how I tried to equalize the two velocities but it doesn't have an effect:
public GameObject bulletObj;
public float bulletSpeed;
Rigidbody rb;
void Awake()
{
rb = GetComponentInParent<Rigidbody>();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
GameObject newBullet = Instantiate(bulletObj, transform.position, transform.rotation) as GameObject;
Rigidbody bulletRb;
bulletRb = newBullet.GetComponent<Rigidbody>();
float gunVelocity = rb.velocity.magnitude + rb.angularVelocity.magnitude;
float finalSpeed = bulletSpeed + gunVelocity;
bulletRb.velocity = transform.forward * finalSpeed;
}
}
What am I doing wrong?
Comment