- Home /
bullet doesn't go anywhere
I made the following script and attached it to my gun, which has a child that is located at the barrel named launch. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Shoot : MonoBehaviour {
private float shootTimer; public float fireRate; [SerializeField] private GameObject launch; [SerializeField] private GameObject bullet; private Vector3 launchLocation;
void Start()
{
fireRate = 0.5f;
launchLocation = launch.transform.position;
}
// Update is called once per frame
void FixedUpdate()
{
if(Input.GetMouseButton(0))
{
if(shootTimer < 0.1f)
{
Instantiate(bullet, launchLocation, Quaternion.identity);
shootTimer = fireRate;
}
else{
shootTimer = shootTimer - 0.1f;
}
}
}
}
Then I have the bullet as a prefab that the gun Instantiates that has this script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bullet : MonoBehaviour { [SerializeField] private float speed; [SerializeField] private float startz; [SerializeField] private float currentz; [SerializeField] private float range; void Start() { startz = transform.position.z; speed = 10f; }
// Update is called once per frame
void FixedUpdate()
{
transform.position += new Vector3(0,0,1);
currentz = transform.position.z;
if(currentz > startz + range)
{
Destroy(this);
}
}
} now, this spawns the bullets stacked on the ground and they don't move at all. This is a early version of the game and this is just about the only thing in the scene besides a capsule representing the player and a camera, well, being a camera. Pls Help. Btw, the serialize field is for debug and won't always be there if that helps.
it sounds like your bullet has a rigid body on it, in that case you should be moving it by adding velocity not changing position. Also you can use Vector3.Distance or Destroy(this, inXSeconds); instead of using currentz
Answer by judahcline · Mar 17, 2021 at 05:05 PM
brackeys does a good job of describng it here: https://www.youtube.com/watch?v=wkKsl1Mfp5M
Your answer
Follow this Question
Related Questions
Calculating 3D Physics Prediction of Shot Direction with Moving target and moving gun (inertia) 1 Answer
When reloading my gun sounds and the bullets glitch out. 2 Answers
Dart/Bullet comes out pointing up 1 Answer
Bullet flies away for no apparent reason. 1 Answer
Please help, bullet drop is wrong at certain angles ? 0 Answers