- Home /
Combine two scripts
Why do when I combine two scripts my Crosshair shows but dont moves. Where is my error ? Who can corretly convert this two scrips ? I mean if click Fire button during reloading the crosshair will move. And Crosshair must move depending on shooting and fire rate. There is the first script:
using UnityEngine;
using System.Collections;
public class Weapon : MonoBehaviour {
//Camera
public Transform _camTransform;
//Animation
public AnimationClip animationIdle;
public AnimationClip animationRun;
public AnimationClip animationFire;
public AnimationClip animationReload;
//Sounds
public AudioClip soundReload;
public AudioClip soundFire;
public float ReloadTime = 3.5f;
public float FireTime = 0.5f;
public float FireRate = 0.5f;
public float Range = 1000;
public int Damage = 50;
public int BulletsInClip = 8;
public int Clips = 3;
public ParticleEmitter muzzleFlash;
private int BulletsLeft = 0;
private float NextFireTime = 0.0f;
private bool Reloading = false;
private bool isFire = false;
private float muzzleFlashLifeTime = 0.0f;
void Start () {
BulletsLeft = BulletsInClip;
}
void Update () {
if (muzzleFlashLifeTime > 0) {
muzzleFlashLifeTime--;
muzzleFlash.emit = true;
} else {
muzzleFlash.emit = false;
}
if (BulletsLeft == 0 && !Reloading && NextFireTime < Time.time) {
Reload ();
}
if (Input.GetButtonDown("Fire1")) {
Fire();
} else if (Input.GetKeyDown(KeyCode.R) ) {
Reload ();
} else if( !Reloading && !isFire ) {
Idle ();
}
}
private void Reload() {
if (Clips == 0) return;
Reloading = true;
Invoke ("ReloadingOf", ReloadTime);
animation.Stop( animationRun.name );
animation.CrossFade( animationReload.name );
audio.PlayOneShot( soundReload );
}
private void Idle() {
if(Input.GetKey( KeyCode.W)) {
animation.CrossFade( animationRun.name );
} else {
animation.CrossFade( animationIdle.name );
}
}
private void Run() {
animation.CrossFade( animationRun.name );
}
private void ReloadingOf() {
BulletsLeft = BulletsInClip;
Clips--;
Reloading = false;
}
private void FireOf() {
isFire = false;
}
private void Fire() {
isFire = true;
Invoke ("FireOf", FireTime);
if (BulletsLeft == 0)
return;
if (Time.time - FireRate > NextFireTime)
NextFireTime = Time.time - Time.deltaTime;
if (NextFireTime < Time.time && BulletsLeft != 0 && !Reloading) {
animation.Rewind ( animationFire.name );
animation.CrossFade( animationFire.name );
audio.PlayOneShot( soundFire );
{
// Debug.Log("fire!");
StaticFunction.GenerateDecal(rayhits);
}
Effects();
NextFireTime += FireRate;
BulletsLeft--;
Vector3 _direction = _camTransform.TransformDirection(Vector3.forward);
/*RaycastHit _hit;
if (Physics.Raycast(_camTransform.position, _direction, out _hit, Range))
{
if (_hit.transform.tag == "Enemy")
{
BotHealth eh = _hit.transform.GetComponent<BotHealth>();
eh.adjustHealth(-Damage, transform);
}
}*/
}
}
private void Effects () {
muzzleFlashLifeTime = 2.0f;
}
void OnGUI() {
int lHeight = 25;
int lWidth = 200;
Rect _pos = new Rect(Screen.width - lWidth, Screen.height - 50, lWidth, lHeight);
Rect _pos2 = new Rect(Screen.width - lWidth, Screen.height - 30, lWidth, lHeight);
string lb = "Bullets: " + BulletsLeft;
GUI.Label(_pos, lb);
string lb2 = "Clips: " + Clips;
GUI.Label(_pos2, lb2);
}
}
There is the crosshair script:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class Proba : MonoBehaviour {
//public preset crosshairPreset = preset.none;
public bool showCrosshair = true;
public Texture2D verticalTexture;
public Texture2D horizontalTexture;
public float cLength = 10;
public float cWidth = 3;
//Spreed setup
public float minSpread = 45.0f;
public float maxSpread = 250.0f;
public float spreadPerSecond = 150.0f;
private Texture temp;
private float spread;
public GUIStyle verticalT;
public GUIStyle horizontalT;
// Update is called once per frame
void Update () {
//Used just for test (weapon script should change spread).
if(Input.GetButton("Fire1")) spread += spreadPerSecond * Time.deltaTime;
else spread -= spreadPerSecond * 2 * Time.deltaTime;
}
void OnGUI(){
if(showCrosshair && verticalTexture && horizontalTexture){
//var verticalT = GUIStyle();
//var horizontalT = GUIStyle();
verticalT.normal.background = verticalTexture;
horizontalT.normal.background = horizontalTexture;
spread = Mathf.Clamp(spread, minSpread, maxSpread);
//Vector2 pivot = new Vector2(Screen.width/2, Screen.height/2);
//if(crosshairPreset == preset.none)
{
//Horizontal
GUI.Box( new Rect((Screen.width - cWidth)/2, (Screen.height - spread)/2 - cLength, cWidth, cLength), temp, horizontalT);
GUI.Box( new Rect((Screen.width - cWidth)/2, (Screen.height + spread)/2, cWidth, cLength), temp, horizontalT);
//Vertical
GUI.Box( new Rect((Screen.width - spread)/2 - cLength, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
GUI.Box( new Rect((Screen.width + spread)/2, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
}
}
}
}
start with smaller scripts? What does the 1st script do,how do you want to combine them?
The first script is gun script. There is animation, sounds. Too that shows bullets in clips, how much clips is left.
If click Fire button during reloading the crosshair will move. And Crosshair must move depending on shooting and fire rate.
Both of these scripts work correctly when used separately, correct?
Yes. I think that I'm not correctly combine that scripts.