- Home /
I am an idiot
Projectile not firing
Yay, back to the same problem. This script, it will not fire when mouse0 is clicked.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AssaultRifle : MonoBehaviour {
public Rigidbody bullet;
public GameObject gun_end;
public float bullet_velocity = 10f;
void Update ()
{
}
void Shoot ()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Rigidbody clone = (Rigidbody)Instantiate(bullet, gun_end.transform.position, gun_end.transform.rotation);
clone.velocity = Vector3.forward * bullet_velocity;
}
}
}
Answer by shadowpuppet · Apr 30, 2018 at 11:02 PM
Mouse0 should be in the Update function, no?
void Update(){
if (Input.GetKeyDown(KeyCode.Mouse0))
Shoot();
}
Answer by KittenSnipes · May 01, 2018 at 08:57 AM
Now it should work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AssaultRifle : MonoBehaviour {
public Rigidbody bullet;
public GameObject gun_end;
public float bullet_velocity = 10f;
void Update ()
{
Shoot();
}
void Shoot ()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Rigidbody clone = (Rigidbody)Instantiate(bullet, gun_end.transform.position, gun_end.transform.rotation);
clone.velocity = Vector3.forward * bullet_velocity;
}
}
}
why put Shoot() in the Update function? It's just saying every frame go to Shoot whether you are shooting or not. why not just put the if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)) in the Update function so it only goes to Shoot if you are shooting
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AssaultRifle : $$anonymous$$onoBehaviour {
public Rigidbody bullet;
public GameObject gun_end;
public float bullet_velocity = 10f;
void Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
Shoot();
}
void Shoot ()
{
Rigidbody clone = (Rigidbody)Instantiate(bullet, gun_end.transform.position, gun_end.transform.rotation);
clone.velocity = Vector3.forward * bullet_velocity;
}
}
}
Follow this Question
Related Questions
OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer
transform.forward not always forward? 2 Answers
Applying force to a rigidbody 2 Answers
Instantiate rigidbody C# 1 Answer
nullrefernceexception 0 Answers