Search Unity

Shifting Projectiles Instantiating point when moving fast.

Discussion in 'Scripting' started by Mikeramczyk1977, Apr 9, 2021.

  1. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    Need help with Side Shifting Projectiles Instantiating point when moving fast. Whenever I move fast in any direction, the instantiating point shifts, sometimes a little, sometimes alot, depending on speed. How do I compensate for player speed to make the instantiating point update its position correctly? Please help?


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Gun : MonoBehaviour
    7. {
    8.     public float speed = 80;
    9.     public GameObject bullet;
    10.     public Transform barrel;
    11.     public AudioSource audioSource;
    12.     public AudioClip audioClip;
    13.  
    14.     public void Fire()
    15.     {
    16.         GameObject spawnedBullet = Instantiate(bullet, barrel.position, barrel.rotation);
    17.         spawnedBullet.GetComponent<Rigidbody>().velocity = speed * barrel.forward;
    18.         audioSource.PlayOneShot(audioClip);
    19.         Destroy(spawnedBullet, 2);
    20.     }  
    21. }
    22.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Are you calling Fire() from a UI button? If so it will be problematic to sync the motion. This is because buttons are updated from Update(), while physics are updated in FixedUpdate().

    To solve your problem, I think all you need to do to the above is:

    1. make a new function to connect to your button instead of
    Fire()
    (let's call that
    RequestFire()
    )

    2. inside
    RequestFire()
    only set a boolean (such as
    fireRequested
    )

    3. Add a FixedUpdate() that checks for
    fireRequested
    to be true, and when it is true:

    ---a. clears
    fireRequested

    ---b. calls
    Fire()
    above.

    For general edification, this gives you an idea of what is going on:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
  3. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Definitely work through half a dozen or so different Youtube tutorials. There are a lot of specific steps that all need to be done 100% correctly.
     
  5. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    I will read up for sure, the script is attached to a gun and triggered by Xr interaction. I am making a very game. I can't assign fire directly to a button.
     
  6. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    It's a VR GAME.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Then there are even more things that a youtube tutorial will cover.
     
  8. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    None of them touches this issue, I have been searching for a while now, and trying to work out these . I fixed the issue of shifting when walking around, but now it shifts the bullet spawning down a couple feet or more when jumping.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Print the coordinates of where you think it should spawn, print the coordinates of where it actually IS spawning, start debugging to find out why they differ. Nothing new here.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you are running a mobile device you can also see the console output. Google for how.
     
  10. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    I've done all that. It was the first thing I thought about was to use debug, and I played with layer interaction, then I played with player controller scripts, I am using VR, this script is attached to a gun, Fire() is called on by Xr grab intractable when holding the gun and pressing trigger.

    This only happens when I jump, so that makes me think that something is happening when I jump, so either it is a script issue caused by the way I am moving my player up ( and I am using character controller) or it has something to do with the firing order of calling the Instantiate from the wrong Update method, or maybe it is the projectile physically interacting with the player.
     
  11. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58

    Well, I am taking the weird Adaptation of reality approach. If something is happening in Unity that is appearing to be controlled by some unknown factor, after debugging and not finding any rational cause for my bullets to instantiate about five feet lower upon pressing the jump button, I figured I will go with another solution, since it only happens when I am jumping and pressing the button assigned to jump in my script, I will use this button pressed function to shift the bullet spawn point up a set amount with a public float, that way I can test and adjust the float amount until the instantiation point lines up correctly while jumping.
     
  12. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58


    Duuuuders!!!! I am a noob, I am creating a VR Game, and I wan't to learn how to make this happen, and what is going on behind it, but I am still a super noob.

    How would I go about this, and how does it work, asa in the method behind it.


    The Script above is attached to a gun, it fires a Projectile that I drag into the Public GameObject field I created.



    The script is called in XR Grab interactables when I push the trigger and when holding the gun obvioulsly.


    The issue is the instantiating is happening behind the times, its instantiating like a couple frames behind me.


    So how would I ammend this code to have the proper update method.
     
  13. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58

    I tried this, I don't know if it will work....


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class Gun : MonoBehaviour
    8. {
    9.     public float speed = 80;
    10.     public GameObject bullet;
    11.     public Transform barrel;
    12.     public AudioSource audioSource;
    13.     public AudioClip audioClip;
    14.     //new
    15.     public GameObject PlayerObj;
    16.  
    17.     private bool fireRequested;
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.         if (!fireRequested)
    22.  
    23.             Fire();
    24.     }
    25.  
    26.  
    27.     public void RequestFire()
    28.     {
    29.         fireRequested = true;
    30.      
    31.     }
    32.  
    33.     public void Fire()
    34.     {
    35.         //new
    36.         //Vector3 playerVelocity = PlayerObj.GetComponent<Rigidbody>().velocity;
    37.         //Vector3 bulletVelocity = new Vector3(playerVelocity.x, playerVelocity.y, playerVelocity.z);
    38.  
    39.         GameObject spawnedBullet = Instantiate(bullet, barrel.position, barrel.rotation);
    40.         spawnedBullet.GetComponent<Rigidbody>().velocity = speed * barrel.forward;
    41.         audioSource.PlayOneShot(audioClip);
    42.         Destroy(spawnedBullet, 2);
    43.     }
    44. }
    45.  
     
  14. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58


    I tried Man, I need a little help here on the clearing the boolean portion, what do I do?


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class Gun : MonoBehaviour
    8. {
    9.     public float speed = 80;
    10.     public GameObject bullet;
    11.     public Transform barrel;
    12.     public AudioSource audioSource;
    13.     public AudioClip audioClip;
    14.     //new
    15.     public GameObject PlayerObj;
    16.  
    17.     private bool fireRequested;
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.         if (!fireRequested == true)
    22.         {
    23.             Fire();
    24.  
    25.  
    26.         }
    27.  
    28.        
    29.     }
    30.  
    31.  
    32.     public void RequestFire()
    33.     {
    34.         fireRequested = true;
    35.         Debug.Log("FireRequested");
    36.     }
    37.  
    38.     public void Fire()
    39.     {
    40.         //new
    41.         //Vector3 playerVelocity = PlayerObj.GetComponent<Rigidbody>().velocity;
    42.         //Vector3 bulletVelocity = new Vector3(playerVelocity.x, playerVelocity.y, playerVelocity.z);
    43.         GameObject spawnedBullet = Instantiate(bullet, barrel.position, barrel.rotation);
    44.         spawnedBullet.GetComponent<Rigidbody>().velocity = speed * barrel.forward;
    45.         audioSource.PlayOneShot(audioClip);
    46.         Destroy(spawnedBullet, 2);
    47.        
    48.     }
    49. }
    50.  
     
  15. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58

    How do I do this:
    ---a. clears
    fireRequested
     
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Wouldn't it just be this:

    Code (csharp):
    1. private void FixedUpdate()
    2.     {
    3.         if (fireRequested)
    4.         {
    5.             fireRequested = false;  // consume input
    6.             Fire();    // do the firing
    7.         }
    8.     }
     
    Mikeramczyk1977 likes this.
  17. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Is your character moving? Add the character's velocity to your bullet's velocity to make it physically accurate, then it should feel proper.
     
    Mikeramczyk1977 likes this.
  18. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    Hey Thanks a million I am trying to learn still.

    So far I have a Splash Intro screen, a Main menu scene, a loading screen, and a first level, so far my player can run and jump and die, and kill the AI enemies I created,they don't just dissappear, they explode and leave behind a burned shell of their robot body that is smoking. Also my player has a life bar. It's a game about a robot that dies and goes to robot hell and has to fight to get out, then goes to all kinds of weird environments / different dimensions. It is crazy as hell so far. Lots of work. I am picking up the coding as I go. I have triggered music changes when you kill the last enemy and then a Boss that comes out, and after you kill him, you go in his head and it's a portal that takes you to another area. It's lots of cussing to getting frustrated and walking away from the computer. Next I am creating my pause menu, I have done it before on another starter project, I got a ways to go. I keep Building and playing it to test for any potential errors or conflicts in the shaders/scripts.

    But thanks
     
    Kurt-Dekker likes this.
  19. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    Can you please show me an example of script to achieve this? I am currently learning as I go. I really want to figure this part out and move on to the next 13 million challenges in coding and putting together my game.
     
  20. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    And yes he is moving, I tried to create a bullet script to achieve this before, but I couldn't figure it out, could you please help me with am example code of how to achieve this, thanks.
     
  21. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58

    Here is the code that is adjusted as you have suggested, does this look correct to you?

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class Gun : MonoBehaviour
    8. {
    9.     public float speed = 80;
    10.     public GameObject bullet;
    11.     public Transform barrel;
    12.     public AudioSource audioSource;
    13.     public AudioClip audioClip;
    14.  
    15.     public bool fireRequested;
    16.  
    17.     void Start()
    18.     {
    19.        
    20.  
    21.     }
    22.  
    23.  
    24.     void FixedUpdate()
    25.     {
    26.        
    27.  
    28.         if (fireRequested)
    29.         {
    30.             fireRequested = false;
    31.             Fire();
    32.             Debug.Log("FireRequested");
    33.  
    34.            
    35.         }
    36.        
    37.  
    38.     }
    39.  
    40.  
    41.     public void RequestFire()
    42.     {
    43.         fireRequested = true;
    44.        
    45.     }
    46.  
    47.     public void Fire()
    48.     {
    49.         //new
    50.         //Vector3 playerVelocity = PlayerObj.GetComponent<Rigidbody>().velocity;
    51.         //Vector3 bulletVelocity = new Vector3(playerVelocity.x, playerVelocity.y, playerVelocity.z);
    52.         GameObject spawnedBullet = Instantiate(bullet, barrel.position, barrel.rotation);
    53.         spawnedBullet.GetComponent<Rigidbody>().velocity = speed * barrel.forward;
    54.         audioSource.PlayOneShot(audioClip);
    55.         Destroy(spawnedBullet, 2);
    56.        
    57.     }
    58. }
    59.  
     
  22. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58


    Just tried the Code, It doesnt woork, it shoots way further ahead and shoots like 3 bullets at once instead of one in a forward direction, No Bueno.........
     
  23. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    I tried Kurts Suggestion and It didnt work, I have another gun script that does kinda work, but it just moves around when I am moving, so If I jump, it shifts the spawnpoint down like 5 feet, or to the left or right depending on my speed.

    Do you know how I can fix this Code to work correctly so the Instantiating point doesnt move around?

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7.  
    8. public class NewGunScript : MonoBehaviour
    9. {
    10.     public float speed = 80;
    11.     public GameObject bullet;
    12.     public Transform barrel;
    13.     public AudioSource audioSource;
    14.     public AudioClip audioClip;
    15.     //new
    16.     //public GameObject PlayerObj;
    17.  
    18.     public void Start()
    19.     {
    20.        
    21.     }
    22.     public void Fire()
    23.     {
    24.         //new
    25.         //Vector3 playerVelocity = PlayerObj.GetComponent<Rigidbody>().velocity;
    26.         //Vector3 bulletVelocity = new Vector3(playerVelocity.x, playerVelocity.y, playerVelocity.z);
    27.  
    28.         GameObject spawnedBullet = Instantiate(bullet, barrel.position, barrel.rotation) as GameObject;
    29.         spawnedBullet.GetComponent<Rigidbody>().velocity = speed * barrel.forward;
    30.         audioSource.PlayOneShot(audioClip);
    31.         Destroy(spawnedBullet, 15);
    32.     }
    33. }
    34.  
    35.