Hello there,
I tried to create a kind of gun script in Unity, I followed orders from a youtube video (which is in german:
)
I already “translated” the script from Javascript to C#.
For some reason, unity says that there are errors in the script, but my script editing program doesn’t…
can someone please help me? Thanks!
Script-------------
// Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
// Do test the code! You usually need to change a few small bits.
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
public class ACE_23 : MonoBehaviour {
GameObject gun;
GameObject soundShot;
float distance;
float maxDistance = 50;
int damage = 25;
static bool isAiming = false;
float fireSpeed = 20;
float waitTilNextFire = 0;
bool bulletHole = GameObject;
bool reloading = false;
Animation reloadAnimation;
AudioSource reloadingSound;
int maxMag = 36;
int currentMag = 36;
int magAfterEmpty = 35;
int maxAmmo = 175;
int currentAmmo = 175;
Text showAmmo;
float spreadFactor;
Transform muzzleFlashSpawn;
GameObject muzzleFlashPrefab;
void Update (){
if(Input.GetButton("Fire2") && reloading == false)
{
isAiming = !isAiming;
if(isAiming == true)
{
GameObject.Find("ACE_23").GetComponent<Animation>().Play("aimdownsight_on");
}
else
{
GameObject.Find("ACE_23").GetComponent<Animation>().Play("aimdownsight_off");
}
}
showAmmo.gameObject.SetActive(true);
showAmmo.text = "Ammo: " + currentMag.ToString() + "/" + currentAmmo.ToString();
if(Input.GetButton("Fire1") && waitTilNextFire <= 0 && currentMag >= 1 && !reloading)
{
if(isAiming == true)
{
GameObject.Find("ACE_23").GetComponent.Animation>(Play("Shoot_ACE23_ads"));
spreadFactor = 10;
}
else
{
GameObject.Find("ACE_23").GetComponent.Animation>(Play("Shoot_ACE23_hip"));
spreadFactor = 50;
}
bool muzzleFlash= Instantiate (muzzleFlashPrefab, muzzleFlashSpawn.position, muzzleFlashSpawn.rotation);
muzzleFlash.transform.parent = gun.transform;
bool shotSound= Instantiate (shotSound, muzzleFlashSpawn.position, muzzleFlashSpawn.rotation);
shotSound.transform.parent = gun.transform;
currentMag --;
waitTilNextFire = 1;
RaycastHit hit;
pos= Vector3(Screen.width / 2, Screen.height / 2, Camera.main.nearClipPlane);
pos += Random.insideUnitCircle * spreadFactor;
ray= Camera.main.ScreenPointToRay(pos);
if(Physics.Raycast (ray, hit, maxDistance))
{
distance = hit.distance;
if(distance < maxDistance)
{
hit.transform.SendMessage("GunDamage", damage, SendMessageOptions.DontRequireReceiver);
}
if(distance < maxDistance && hit.transform.tag == "Level_Object")
{
Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
}
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
if(waitTilNextFire <= 0)
{
waitTilNextFire = 0;
}
//AMMO
if(currentMag > maxMag)
{
currentMag = maxMag;
}
if(currentAmmo > maxAmmo)
{
currentAmmo = maxAmmo;
}
if(Input.GetButtonDown(KeyCode.R) && !reloading && currentMag < maxMag && currentAmmo > 0)
{
reloading = true;
isAiming = false;
GameObject.Find("ACE_23").GetComponent.Animation>(Play("ace_23_reload"));
reloadingSound.Play();
}
if(Input.GetButtonDown("Fire1") && !reloading && currentMag == 0)
{
GameObject.Find("ACE_23").GetComponent.Animation>(Play("ace_23_empty"));
}
if(reloading == true && !reloadAnimation.IsPlaying("ace_23_reload"))
{
if(currentMag <= 0)
{
if(currentAmmo >= maxMag - currentMag)
{
currentAmmo -= magAfterEmpty - currentMag;
currentMag = magAfterEmpty;
}
if(currentAmmo >= maxMag - currentMag)
{
currentAmmo -= maxMag - currentMag;
currentMag = maxMag;
}
if(currentAmmo < maxMag - currentMag)
{
currentMag += currentAmmo;
currentAmmo = 0;
}
}
reloading = false;
}
}
}