- Home /
velocity of bullet in rifile rotation
Hi all,
I am beginner to unity 2d,I am trying to rotate a gun and shoot bullets in the gun direction in unity 2d. ! have two scripts: below one is for barrel point.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BarrelScript : MonoBehaviour {
public Transform barrel;
public Rigidbody2D bullet;
public float fireSpeed = 500f;
// Use this for initialization
// Update is called once per frame
void Update () {
Fire ();
}
void Fire ()
{
if (Input.GetButtonDown("Fire1")) {
Rigidbody2D firedBullet = Instantiate (bullet, barrel.position, barrel.rotation) as Rigidbody2D;
firedBullet.AddForce (barrel.up * fireSpeed);
}
}
}
another script for rotating gun I am able to rotate the gun but my problem is I am not able to shoot the bullet in barel position.
please help me out. Thank you.
Answer by Lardalot · May 20, 2018 at 10:35 AM
Are your bullets flying up instead of forwards? You're adding force in the up direction of the barrel, which would normally be vertical. You probably want the forward direction, which should be down the Z axis of your barrel.
firedBullet.AddForce (barrel.forward * fireSpeed);
Your answer
Follow this Question
Related Questions
Optimizing Code 1 Answer
Ship won't shoot right in SMHUP. Help?? 2 Answers
bullets are spawning under me and they don't move 1 Answer
Random Raycast inside crosshairs 1 Answer
Best way to shoot physical bullets? 2 Answers