Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity Gun Problems JS

Discussion in 'Scripting' started by Fireking883, Oct 11, 2015.

  1. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    I have tried looking all over the internet for a fix and have spent a long time trying to fix the problem myself, so before you say look it up, I have. My problem is that the bullet I instantiate is not going forward, but to the right or at a 45 degree angle up from where it is supposed to shoot from. I want the bullet to shoot straight forward, but I cannot figure out how to do this. I have created an empty gameobject at the end of my gun and instantiate from there. Here is my script:
    Code (JavaScript):
    1. #pragma strict
    2. var Barrel : Transform;
    3. var BarrelPos : Transform;
    4. var Bullet1 : GameObject;
    5. function Update () {
    6.     if (Input.GetKey(KeyCode.LeftArrow)){
    7.           Barrel.Rotate(Vector3.back, Space.Self);
    8.     }
    9.     if (Input.GetKey(KeyCode.RightArrow)){
    10.          Barrel.Rotate(Vector3.forward, Space.Self);
    11.     }
    12.     if (Input.GetKeyDown(KeyCode.Space)){
    13.         var Bullet = Instantiate(Bullet1, BarrelPos.position,  BarrelPos.rotation) as GameObject;
    14.         Bullet.GetComponent.<Rigidbody>().AddForce(BarrelPos.position * 1000);
    15.     }
    16. }
    Thanks in advance for help!
     
  2. Fireking883

    Fireking883

    Joined:
    Nov 28, 2014
    Posts:
    50
    I have seemed to of fixed it by locking the y position on the bullet, so no need to answer.