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

Issues With Firing Projectile While Moving

Discussion in 'Scripting' started by Tetra, Apr 11, 2017.

  1. Tetra

    Tetra

    Joined:
    May 22, 2013
    Posts:
    29
    Maybe I'm tired, maybe I'm blind, or maybe something else is going on... but I'm having an issue that I haven't seen answered anywhere yet (that I've found, at least).

    When I shoot a projectile from my player while moving side-to-side, the projectile looks like it is coming from the side of the player. I thought about adding the players speed into the position calculation, but then that would cause the projectile to move in a direction other than straight forward.

    Here's my movement code:

    Code (CSharp):
    1.  
    2. // Snip...
    3.  
    4.         if (controller.isGrounded)
    5.         {
    6.             moveDirection = new Vector3(horizontal, 0, 0.8f);
    7.             moveDirection = Transform.TransformDirection(moveDirection);
    8.             moveDirection *= currentSpeed;
    9.         }
    10.  
    11.  
    12.         moveDirection.y -= gravity * Time.deltaTime;
    13.         controller.Move(moveDirection * Time.deltaTime);
    14.  
    In this code, controller is a variable of type CharacterController.


    Here's my code for shooting:

    Code (CSharp):
    1.  
    2. public class Shoot : MonoBehaviour
    3. {
    4.     public GameObject player;
    5.     public GameObject bulletPrefab;
    6.     public Transform bulletSpawner;
    7.  
    8.     void Update()
    9.     {
    10.         if (Input.GetKeyDown(KeyCode.Space))
    11.             Fire();
    12.     }
    13.  
    14.     void Fire()
    15.     {
    16.         GameObject obj = GameObject.Instantiate(bulletPrefab, bulletSpawner.position, bulletSpawner.rotation);
    17.     }
    18. }
    19.  

    And finally, here is the movement code for the bullet projectile

    Code (CSharp):
    1. public class Bullet : MonoBehaviour
    2. {
    3.     void Awake()
    4.     {
    5.         Destroy(gameObject, 2f);
    6.     }
    7.  
    8.     void Update()
    9.     {
    10.         Vector3 moveDir = new Vector3(0, 0, 1);
    11.         transform.position += moveDir * Time.deltaTime * 40;
    12.     }
    13. }

    And, here's a picture of what is currently happening, for those of you that would like a visual representation.


    Any assistance with this would be greatly appreciated.
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    It's hard to know what your problem would be with what you're provided.

    You might be having an update order issue. Your could be firing before your bulletSpawner is moving, then your bulletSpawner moves after and so the bullet is no longer fired from the bulletSpawner transform position. try changing Shoot's Update() to LateUpdate(). This is only going to be noticeable at low framerates.

    Your bulletSpawner transform could be in the wrong place. If the bullet spawns in the center of your player then by the time the bullet moves to the front of the player the player has moved to the left/right and the bullet is no longer aligned with the barrel. You'll need to spawn the bullet at a half bullet length in front of the end of the players' barrel.
     
    Tetra likes this.
  3. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    I have the answer to fix this ABSOLUTELY 1000000000000%%%%%%%%%%%%%%%%%%%
     
  4. gurbrindersingh

    gurbrindersingh

    Joined:
    Feb 28, 2017
    Posts:
    32
  5. Yrandika

    Yrandika

    Joined:
    Sep 17, 2018
    Posts:
    2
    What was the answer, I am having the same issue with my project. I could not make it. plz help
     
  6. unity_fqlyjbgDafPWCw

    unity_fqlyjbgDafPWCw

    Joined:
    Feb 5, 2021
    Posts:
    9
    You could try instantiating the bullet in LateUpdate somehow, maybe call Fire() there. I have a grappling hook that has to render its rope in late update in order to avoid a similar issue, but I'm not sure how well your controls will work if you put those in LateUpdate().