So i was trying to add bullets into my game and i made a code
and i dont know what to do to fix this code
var Bullet : GameObject;
var Spawn : Transform;
function Update ()
{
if(Input.GetButtonDown[‘fire1’)){
fire[ ];
}
}
function fire()(
instancelate(Bullet.Spawn.Position.Spawn.Rotation);
}
This is For the m4 and i am going to implement a ak47
If your coding for a gun you probably want it to use raycast and not a bullet object.
I’ll give you a basic(but working) unityscript that I use. It’s for one shot weapons since you didn’t say what
type of weapon you want. May not be the best but it works.
var shotSound : AudioClip; // Shot sound
var bloodPrefab : GameObject; // Blood for enemy
var sparksPrefab: GameObject; // Sparks for groun
var muzzleFlash : GameObject;
var shootEnabled: boolean = true; // allows disabling the shots
var ChangeWep : boolean = true;
var RayCastDist : float = 50;
var Bullets = 9;
var MaxBullets = 9;
var Clip = 9000;
var force = 50;
var Recoil = 10;
var Accuracy = 50;
var shotInterval = 1.0;
var Cost = 50;
var Zoom : int;
function Start(){
Zoom = (PlayerPrefs.GetInt("FOV") - 5);
muzzleFlash.renderer.enabled = false;
shootEnabled = true; // allows disabling the shots
ChangeWep = true;
}
function Update(){
var forward : Vector3 = transform.TransformDirection(Vector3.forward) * 10;
if (Input.GetButtonDown("Fire1")){
if (shootEnabled == true){
if (Bullets > 0){
if (Clip > 0){
Shoot();
Bullets -= 1;
}
}
}
}
if (Input.GetButtonUp("Fire1")){
ChangeWep = true;
}
if (Bullets == 0){
if (Clip >= 1){
Clip -= 1;
Bullets = MaxBullets;
}
if (Clip == 0){
Bullets = 0;
}
}
}
function Shoot(){
var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
var trf = transform; // a little optimization
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
audio.PlayOneShot(shotSound); // play the shot sound...
MuzzleFlashOn();
if(Physics.Raycast (ray, hit, RayCastDist)){
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(hit.collider.gameObject.tag == "Box"){
Debug.Log ("Hit A Box!");
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
hit.rigidbody.AddForceAtPosition(force * transform.forward , hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", 5.0);
}
if(hit.collider.gameObject.tag == "Enemy"){
Debug.Log ("Hit A Enemy!");
hit.collider.gameObject.BroadcastMessage("LoseHealth", force * 2 / 4);
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
}
if(hit.collider.gameObject.tag == "Target"){
Debug.Log ("Target hit!");
hit.rigidbody.AddForceAtPosition(force * transform.forward , hit.point);
hit.rigidbody.isKinematic = false;
hit.rigidbody.useGravity = true;
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
} else {
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
Debug.Log ("Hit Other!");
}
ChangeWep = false;
SendMessageUpwards("Recoil");
shootEnabled = false;
yield WaitForSeconds(shotInterval);
shootEnabled = true;
ChangeWep = true;
}
}
function MuzzleFlashOn()
{
var muzzleFlashLength = 0.1;
muzzleFlash.renderer.enabled = true;
yield WaitForSeconds(muzzleFlashLength);
muzzleFlash.renderer.enabled = false;
}