Problem of shooting while moving

Hello,

i have a problem of the shooting on the game i work. It’s a game of Snowmen shooting Snowball, in a Mario Party vibe.

I coded the shooting of the snowball, here the code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;


public class PlayerShoot : MonoBehaviour
{
    [SerializeField] private GameObject SnowPrefab;

    public void Fire(InputAction.CallbackContext context)
    {
        if (context.interaction is PressInteraction)
        {
            if (context.performed)
            {
                //create snowBall in front of snowMan
                Instantiate(SnowPrefab, this.transform.position + this.transform.forward, this.transform.rotation);
            }
        }


    }
}

And the shoot works… if you don’t move. Because if you move while shooting, this happens :

I just started C#, did you have any idea of what can i do ?

I mean, it’s doing exactly what you told it to. It spawns one unit in front of the player with the same rotation as the player. The problem is probably in some other script. The code you posted just spawns the bullet, but where do you actually make it move? Depending on that it might be either wrong to give the bullet the player’s rotation, or the direction it starts moving in isn’t correct.

Well, it’s the line of

Instantiate(SnowPrefab, this.transform.position + this.transform.forward, this.transform.rotation);

but the thing is that it’s not really the mouvement, it’s more like transformed the position.

I don’t understand what you mean. That line has absolutely nothing that’s moving the bullet in any way. It’s just spawning it and that’s all it’s doing.

@ThibaudJosesh It looks correct to me too. You might place these lines just before instantiating to confirm the values are what you expect, they will show in the Console window:

Debug.Log("Position = " + this.transform.position.ToString());
Debug.Log("Forward = " + this.transform.forward.ToString();
Debug.Log("Rotation = " + this.transform.rotation.ToString();