- Home /
Shoot. Just Shoot
This is a very simple question but... can you give me a script so when i hit a key it shoots a certan game object.
Thanks!!!
(LEAVE INSTRUCTIONS!)
Please mark Eric's answer right as it is perfectly correct and likely the only answer you will get. Also, don't ask for scripts. Ask for help with scripts you are making.
@killer1390, don't expect a reply/checkmark. Some posters are like pandas - "eats, shoots, leaves"...
Answer by Eric5h5 · May 07, 2010 at 07:02 PM
Have a look at the first-person shooter tutorial.
Just a comment that the above link does not work. It redirects you to demos.
Answer by LFZ2 · Nov 17, 2010 at 02:48 PM
public var snowballPrefab : Transform; public var snowballSpeed : float = 6000;
function Update(){ if(Input.GetButton("Fire1")) { if(!snowballPrefab || !snowballSpeed) { Debug.Log("[Shoot] 'bulletPrefab' or 'bulletSpeed' is undefined"); }else{ var snowballCreate = Instantiate(snowballPrefab, GameObject.Find("SSpawnPoint").transform.position, Quaternion.identity); snowballCreate.rigidbody.AddForce(transform.forward * snowballSpeed); } } }
Try this, replace snowball with your game object. I am still trying to find out a better way:)
Fire 1 is the left mouse button :( See the keys in the Edit> Player Settings> Input
Answer by lblaz7 · Jun 02, 2012 at 08:23 AM
dont ask for scripts use the references and learn it yourself
Answer by Bruno-Fludzinski · Jun 25, 2012 at 04:08 AM
put A empty game object in front of your gun ( where u want to shoot from) and name it "spawnPoint" then attach this script:
var fireRate : float = 0.1;
var Prefab: Transform;
private var nextFire = 0.0;
var speed : float = 50;
function Update () {
if(Input.GetKey("mouse 0")&&Time.time > nextFire){
nextFire = Time.time + fireRate;
var copy = Instantiate(Prefab,GameObject.Find("spawnPoint").transform.position,transform.rotation);
copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed);
}
}
to the player, you can edit the fire rate, in the inspecter panel make sure to when it says prefab, add a game object there, such as a cube or sphere.
Answer by citizen_rafiq · Aug 30, 2013 at 02:12 PM
// attach this script with your gun and put an empty game object as a child of gun which work as spawn point of bullet
public class AutoFire : MonoBehaviour {
public float frequency = 10;
public float coneAngele = 1.0f;
public bool firing = false;
public float hitSoundVolume = 5f;
public GameObject muzzleFlash;
public AudioClip soundHit;
private float _lastFireTime = -1;
private RaycastHit _hitInfo;
private Quaternion _coneRandomRotation;
private Vector3 _force;
private GameObject _bullet;
private Transform _spawnPoint;
RaycastHit hitInfo;
void Start () {
if(muzzleFlash) muzzleFlash.active = false;
_force = new Vector3(0,0,0);
_bullet=Resources.Load("Prefabs/bullet", typeof(GameObject))as GameObject;
if(!_bullet){
Debug.LogError("keep bullet prefab in Resources/Prefabs/bullet");
}
foreach(Transform tr in transform){
if(tr){
_spawnPoint=tr;
}
}
}
// Update is called once per frame
void Update () {
if (firing) {
if (Time.time > _lastFireTime + 1 / frequency) {
//coneRandomRotation= Quaternion.Euler (Random.Range(-coneAngele, coneAngele), Random.Range(-coneAngele, coneAngele),0);
_coneRandomRotation = Quaternion.Euler(0, 0, 0);
GameObject go= Instantiate(_bullet,_spawnPoint.position,Quaternion.Euler(Vector3.zero)) as GameObject;
SimpleBulletOrMissile bulletOrMissile = go.GetComponent<SimpleBulletOrMissile>();
_lastFireTime = Time.time;
Physics.Raycast (go.transform.position, go.transform.up,out _hitInfo,Mathf.Infinity,-5);
if (_hitInfo.transform)
{
print(_hitInfo.transform.tag+"hitted");
Health targetHealth = _hitInfo.transform.GetComponent<Health>();
if (targetHealth)
{
//kill or health damage of target
}
if (_hitInfo.rigidbody)
{
//if want to apply force on enemy
hitInfo.rigidbody.AddForceAtPosition(_force, hitInfo.point, ForceMode.Impulse);
}
AudioSource.PlayClipAtPoint(soundHit, _hitInfo.point, hitSoundVolume);
bulletOrMissile.dist = _hitInfo.distance;
}
else {
bulletOrMissile.dist = 50;
}
}
}
if(Input.GetMouseButton(0)){
OnStartFire();
}
if(Input.GetMouseButtonUp(0)){
OnStopFire();
}
}
void OnStartFire() {
if (Time.timeScale == 0)
return;
firing = true;
if(muzzleFlash) muzzleFlash.active = true;
if (audio) audio.Play();
}
void OnStopFire() {
firing = false;
if(muzzleFlash) muzzleFlash.active = false;
if (audio) audio.Stop();
}
//attach this script with bullet
public class SimpleBulletOrMissile : MonoBehaviour {
public float speed = 10; public float lifeTime = 0.5f; public float dist = 100;
private float _spawnTime = 0.0f;
private Transform _tr;
// Use this for initialization
void OnEnable () {
_tr = transform;
_spawnTime = Time.time;
}
// Update is called once per frame
void Update () {
_tr.position += _tr.forward * speed * Time.deltaTime;
dist -= speed * Time.deltaTime;
if (Time.deltaTime > _spawnTime + lifeTime || dist < 0) {
// print("Destroy form bullet");
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
Clones of enemies will not shoot 0 Answers
Enemy fire does not propel in any direction? 1 Answer
Enemy shoot 1 Answer
Bullet not shooting enemy center in unity 1 Answer
How do I make the enemy stop walking and shoot the player? 1 Answer