Instantiation Problem While Moving

I’m making a game that is going to incorporate projectile bullets. My issue is that when I am moving around, the bullets start to exit next to the gun barrel. Like it’s lagging the position. Is there a way to more accurately track the position of the barrel tip transform so that when I am moving it will instantiate at the barrel tip, not a foot away from the gun. Here’s my script, thanks in advance. (PS- Ignore the timescale changer)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PistolScript : MonoBehaviour
{
public Animator anim;
public AudioSource audio;
public Camera cam;
public float range;
public GameObject bullet;
public Transform tip;
// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.V))
{
Time.timeScale = 0.3f;
if (Input.GetKeyUp(KeyCode.V))
{
Time.timeScale = 1f;
}}
if (Input.GetMouseButtonDown(0))
{
Shoot();
anim.Play(“fire”);
audio.Play();
}
}
void Shoot()
{
GameObject cap = Instantiate(bullet, tip.position, Quaternion.identity);
cap.transform.position = tip.position;
}
void BackUp()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
}
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
Rigidbody rig;
public float force;
// Start is called before the first frame update
void Start()
{
rig = GetComponent();
transform.Rotate(90, 0, 0);
rig.AddRelativeForce(Vector3.forward * force);
}
// Update is called once per frame
void Update()
{

}
public void OnCollisionEnter(Collision collision)
{
Debug.Log(collision.transform.name);
if(collision.transform.name != “Player”)
{
Destroy(this.gameObject);
}
}
}

Hi @kiop2336 , please use code tags when adding code snippets in the forums, it makes it much easier to read.

The code looks ok from what I can see, is the “tip” transform a child of the gun transform?
It might be helpful to see a screenshot of your object hierarchy in the editor.

Also, I see your bullet object is being rotated, can we see what the bullet prefab looks like?
If the position in the prefab is not set to 0,0,0 sometimes it won’t instantiate in the expected position.

2 Likes

Hey mate, this is not a bug.

This is a common behavior for physical projectiles.
Some things you can do to improve.

  • Slow your player down

  • Instantiate a little in front of the barrel tip. Visually less noticeable

  • Make your bullets faster(though this can create other visual defects)

Otherwise, don’t use physical projectiles.

Most games wont actually use physical projectiles. Because of this issue. It is pretty accurate, its just your eyes. Its up to the game designer to come up with a way to make this less noticeable. For example, a trial render that starts from infront of the barrel, and VERY quickly dissapears. In’s not directly infront of the barrel so the player wont notice, and as its only visable for a split secon, they wont notice the “static” look of the trail as they run arround.

Look at Overwatch for example, AAA game but still has that issue. Most characters don’t have fast repeating physical projectiles. Except for Anna, look at game-play of her. The have a physical trail from the bullet that can look defected.

1 Like

A million thanks guys. Thanks for the tip, I will start thinking of a way to speed up the bullet to make it less visible to the eye (Well kinda like a real bullet lol). Am also thinking of making a system to move the barrel tip according to the direction you move to make it instantiate from what looks like the actual barrel. I also have a dash feature, so I will have to change the distance the barrel tip moves left/right according to speed.

If you instantiate the bullet at the yellow dot, a little in front. And a particle effect at the barrel tip. This should look much better.