Search Unity

Question The problem with the interpolation of a bullet fired from a moving object

Discussion in 'Netcode for GameObjects' started by d1mentor, Mar 24, 2024.

  1. d1mentor

    d1mentor

    Joined:
    Jun 27, 2021
    Posts:
    5
    Hello. I'm trying to get a smooth shot of a moving car. When the car is at rest (that is, just standing still) and shooting, everything works correctly (with the exception of a small delay at the very beginning of the movement of the bullet, but this is not critical). But when the car starts to move and at the same time fires a shot, the impression is created that the bullet spawns in front of the car and the position is interpolated a little back, but then aligns. This is clearly visible in the video I am attaching.


    Please, if anyone has encountered this, or knows how to solve the "bullet interpolation in reverse direction" when shooting in motion.
    I have been trying to solve this problem for about two weeks now. I would greatly appreciate your help in solving this problem.



    Here are the NetworkTransform settings and the code that moves the bullet.
    upload_2024-3-24_12-6-8.png
    Code (CSharp):
    1. public class NetworkBullet : NetworkBehaviour
    2.     {
    3.         [Inject] private BulletBehavior bulletBehavior;
    4.    
    5.         [SerializeField] private float speed;
    6.         [SerializeField] private float damage;
    7.  
    8.         private void Update()
    9.         {
    10.             transform.Translate(Vector3.forward * (speed * Time.deltaTime));
    11.         }
    12.    
    13.         public override void OnNetworkSpawn()
    14.         {
    15.             base.OnNetworkSpawn();
    16.             bulletBehavior.Initlialize(this);
    17.         }
    18.     }
    Link to video
    https://drive.google.com/file/d/1rBWytSJ2Y-abEHSkfsg0zM8rO7oO-gU5/view?usp=sharing
     

    Attached Files:

    Last edited: Mar 24, 2024
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,025
    Video doesn't play for me.

    The code doesn't show how the bullet gets spawned. Perhaps the initial position is off. If the firing position is right in front of a car and a client car is moving forward, you should notice that the rocket actually spawns a little inside the car depending on lag.

    Try disabling Interpolation for debugging to really see what's going on.
     
  3. d1mentor

    d1mentor

    Joined:
    Jun 27, 2021
    Posts:
    5
    I have uploaded the video to my google drive, please try again to access it. Regarding the code that instantiates the bullet, I don't think it will be useful. After all, when the car is not moving, there are no visual problems with the movement of the bullet.

    https://drive.google.com/file/d/1rBWytSJ2Y-abEHSkfsg0zM8rO7oO-gU5/view?usp=sharing
     
    Last edited: Mar 24, 2024
  4. d1mentor

    d1mentor

    Joined:
    Jun 27, 2021
    Posts:
    5
    The bullet appears exactly in front of the car when it moves forward, but in the following frames it seems to be approaching the car (this should not be the case, because the speed of the bullet is higher than the speed of the car). Please let me know if the video is not available for you again.

    P.S. With interpolation off, the bullet moves correctly and does not get close to the car after being shot. But it appears very far ahead of the car and moves in jerks.