I am trying to make a first person shooting game, however I am a newbie to unity and don’t know how it works. The bullet falls straight down after I start shooting it. Here below is the script I use for my character, what I can do to improve the situation?
if(Input.GetButtonDown("Jump"))
{
var bullit = Instantiate(bullitPrefab,
transform.Find("Spawn1").transform.position,
Quaternion.identity);
rigidbody.AddForce(transform.forward * 2000);
transform.position += Time.deltaTime * speed * transform.forward;
}
Force is added in Fixed update, it will move your bullet forward, so you don’t need transform.position += ...
Something like this should help you out:
Place this one your player:
public class Shoot : MonoBehaviour{
public float shootRate = 1f;
public GameObject bullitPrefab;
protected GameObject spawnPoint;
protected bool shooting = false;
void Start(){
spawnPoint = transform.Find("Spawn1");
StartCoroutine(shoot());
}
IEnumerator shoot(){
while(true){
if(shooting){
Instantiate(bullitPrefab, spawnPoint.transform.position, spawnPoint.transform.rotation);
}
yield return new WaitForSeconds(shootRate);
}
}
void Update () {
if(Input.GetButtonDown("Jump")){
shooting = true;
}else{
shooting = false;
}
}
}
And place this on your bullet Prefab:
public class Bullet : MonoBehaviour{
public float bulletSpeed = 1f;
void FixedUpdate () {
rigidbody.AddForce(Vector3.forward * Time.deltaTime * bulletSpeed);
}
}