Need some Help with an old project. Not working anymore

Hey Guys.
I just started to get into unity again and my “old” project was made in unity 3 and now in unity 4 its not working anymore.

I can hardly say whats wrong because a friend of mine wrote the script and he is not reachable anymore. so, as i just wanted to continue with the project i wanted to ask you if you can have a look at the project? i have uploaded it to my mega account:

so, basically you have to open the project, and in the main folder you find a “game” scene. there you could (normally) control the character, switch weapons, shoot, use a flashlight, and so on. but yeah… its not working :confused:

so, i would be really thankful if you might have look at it.
as reward you could also use any asset from the game if you want for your use.

Cheers!

What happens when you open the project ? Do you get a bunch of errors ? Does the main scenes open at all. Not many people will download a project they don’t know anything about. Best of luck on that. The norm would be for you to post code you have errors with and fix it one script at a time. Also you can try to use the assets to build your scenes over. Do all your prefabs still have all their script components ?

Hey, thanks for your answer.
Yes, i can open the project and test the scene. and i have just 2 “errors”

  1. The animation state Idle_Aim_2 could not be played because it couldn’t be found!

  2. NullReferenceException: Object reference not set to an instance of an object
    CameraController.LateUpdate () (at Assets/Main/Scripts/Controls/CameraController.cs:18)

  3. I dont think this could be the reason why i cant control the character at all

  4. didnt realy understand this error message

Your errors in the console should give you the line and character count to the error though the compiler sometime just comes close to the actual line number and location - lol. Find the object that uses the Idle_Aim_2 animation and check in the inspector to so if the model needs to be re imported or setup differently with the new animation import settings. The problem may be as simple as assigning the animation in the inspector. The Null Reference Exception error is from a var not being assigned or a component or object that is found through script can not be located. This may be because an item is not tagged or has been renamed.

Hey,
jeah. the problem is, there is no Idle_Aim_2 State

The Rerefrenced line looks like this:

void UpadateMotionState(){
		if(_grounded){
			if(inputAxis1.y == 0  inputAxis1.x == 0)
				motionState = aiming ? MotionStates.Idle_Aim : MotionStates.Idle_Simple;
			
			if(inputAxis1.y == 0  inputAxis1.x > 0)
				motionState = aiming ? MotionStates.Walk_Right : MotionStates.Run_Right;
			
			if(inputAxis1.y == 0  inputAxis1.x < 0)
				motionState = aiming ? MotionStates.Walk_Left : MotionStates.Run_Left;
	
			if(inputAxis1.y > 0  inputAxis1.x == 0)
				motionState = aiming ? MotionStates.Walk_Forward : MotionStates.Run_Forward;
	
			if(inputAxis1.y < 0  inputAxis1.x == 0)
				motionState = aiming ? MotionStates.Walk_Back : MotionStates.Run_Back;
	
			if(inputAxis1.y > 0  inputAxis1.x > 0)
				motionState = aiming ? MotionStates.Walk_Forward_Right : MotionStates.Run_Forward_Right;
	
			if(inputAxis1.y < 0  inputAxis1.x < 0)
				motionState = aiming ? MotionStates.Walk_Back_Left : MotionStates.Run_Back_Left;
	
			if(inputAxis1.y < 0  inputAxis1.x > 0)
				motionState = aiming ? MotionStates.Walk_Back_Right : MotionStates.Run_Back_Right;
	
			if(inputAxis1.y > 0  inputAxis1.x < 0)
				motionState = aiming ? MotionStates.Walk_Forward_Left : MotionStates.Run_Forward_Left;
			}
		else{
			motionState = MotionStates.Jump;
		}

so, just Idle_aim

i just cleaned the project. just 5mb this time:

(use the testlevel scene in the main asset folder)

the idle_aim_2 error is solved but i have this new errors:
NullReferenceException: Object reference not set to an instance of an object
GTA_Controller.get_IS_CROUCHING () (at Assets/Scripts/Network/GTA_Controller.cs:146)
GTA_Controller.Update_Animator () (at Assets/Scripts/Network/GTA_Controller.cs:304)
GTA_Controller.Update () (at Assets/Scripts/Network/GTA_Controller.cs:244)

GTA_Controller.cs

using UnityEngine;
using System.Collections;

[System.Serializable]
public class WeaponSystemSetup{
	public Weapon_Slot[] slots;
	public Transform weaponBone;
	public LayerMask hitLayers;
	public Hit_Event[] hitEvents;
	
	[HideInInspector] public Weapon_Slots currentSlot, lastSlot;
	[HideInInspector] public float NEXT_SELECTION_EVENT;
	
	[System.Serializable]
	public class Weapon_Slot{
		public Weapon_Slots SLOT;
		
		[HideInInspector] public int ID;
		[HideInInspector] public bool USED = false;
		[HideInInspector] public GTA_WeaponManager WEAPON_OBJECT;
		
		[HideInInspector] public int AMMO_IN_CLIP;
		[HideInInspector] public int HAS_AMMO;
		[HideInInspector] public float NEXT_ATTACK_EVENT;
		[HideInInspector] public float NEXT_RELOAD_EVENT;
		
		public bool HAS_WEAPON_OBJECT{
			get{
				return WEAPON_OBJECT ? true : false;
			}
		}
		
		public GTA_Weapons.Weapon_Flags FLAGS{
			get{
				return GTA_Weapons.GET_WEAPON_SLOT_FLAGS(SLOT, ID);
			}
		}
		
		public GTA_Weapons.Weapon_Info WEAPON_INFO{
			get{
				return GTA_Weapons.GET_WEAPON_INFO(SLOT, ID);
			}
		}
	}
	
	[System.Serializable]
	public class Hit_Event{
		public string hitTag;
		public Transform root;
		public ParticleEmitter[] emittOnHit;
		public AudioClip clip;
	}
	
	public enum Weapon_Slots{
		UNARMED,
		MELEE,
		HANDGUN,
		SHOTGUN,
		RIFLE,
	 	SNIPER,
		HEAVY,
	}
	
	public bool CLIP_EMPTY{
		get{
			return !(slots[(int)currentSlot].AMMO_IN_CLIP > 0);
		}
	}
	
	public bool CLIP_FULL{
		get{
			return slots[(int)currentSlot].AMMO_IN_CLIP == (int)slots[(int)currentSlot].WEAPON_INFO.CLIP_SIZE;
		}
	}
	
	public bool HAS_AMMO{
		get{
			return slots[(int)currentSlot].HAS_AMMO > 0;
		}
	}
	
	public bool IS_SELECTION{
		get{
			return !(Time.fixedTime >= NEXT_SELECTION_EVENT);
		}
	}
	
	public bool IS_ATTACKING{
		get{
			return !(Time.fixedTime >= slots[(int)currentSlot].NEXT_ATTACK_EVENT);
		}
	}
	
	public bool IS_RELOADING{
		get{
			return !(Time.fixedTime >= slots[(int)currentSlot].NEXT_RELOAD_EVENT);
		}
	}
	
	public bool CAN_AIM{
		get{
			return slots[(int)currentSlot].FLAGS.CAN_AIM  !IS_RELOADING  !IS_SELECTION;
		}
	}
	
	public GTA_Weapons.Weapon_Motion_Data CURRENT_WEAPON_MOTION_DATA{
		get{
			return GTA_Weapons.GET_WEAPON_MOTION_DATA(currentSlot, slots[(int)currentSlot].ID);
		}
	}
}

public class GTA_Controller : MonoBehaviour {
	public bool SHOW_DEBUG_INFO = true;
	public Animator a;
	public float mouseXSens = 1.0f, mouseYSens = 1.0f;
	public Transform[] upperBodyChain;
	public float V_LookAngleThreshold = 10.0f;
	
	public WeaponSystemSetup weapons;
	
	[HideInInspector]	public bool SYNC_is_aiming;
	//[HideInInspector]	public bool SYNC_is_attacking;
	[HideInInspector]	public bool SYNC_is_reloading;
	[HideInInspector]	public float SYNC_animator_move_speed;
	
	[HideInInspector]	public float h, v, mouseX, mouseY;
	[HideInInspector]	public float V_LookAngle;
	[HideInInspector]	public float aimingWeightControl;
	[HideInInspector]	public GTA_CameraOrbit c;
	
	public static CustomInput cInput;
	
	Vector3 rotateDirection_current;
	Vector3 rotateDirection_target;
	Vector3 moveDirection_current;
	Vector3 cameraForward;
	
	public GUIStyle UIStyle;
	
	public bool isGrounded;
	public GameObject decalPrefab;
	
	public bool IS_CROUCHING{
		get{
			return Input.GetKey(GTA_Controller.cInput.KEY_TOGGLE_CROUCH);
		}
	}
	
	public bool IS_AIMING{
		get{
			return weapons.CAN_AIM ? Input.GetKey(GTA_Controller.cInput.KEY_TOGGLE_VIEW) : false;
		}
	}
	
	public float ANIMATOR_MOVE_SPEED{
		get{
			return IS_MOVING ? (Input.GetKey(GTA_Controller.cInput.KEY_TOGGLE_RUN_WALK)  !weapons.IS_RELOADING ? 1.0f : 0.5f) : 0.0f;
		}
	}
	
	public bool IS_MOVING{
		get{
			return !(h == 0  v == 0);
		}
	}
	
	public float VELOCITY_MULTIPLER{
		get{
			if(IS_MOVING){
				if(!IS_CROUCHING)
					return IS_AIMING ? 1.3f : (Input.GetKey(cInput.KEY_TOGGLE_RUN_WALK)  !weapons.IS_RELOADING ? 3.4f : 1.5f);
				return IS_AIMING ? 0.9f : 1.1f;
			}
			return 0.0f;
			//return IS_MOVING ? (IS_AIMING ? 1.3f : (Input.GetKey(cInput.KEY_TOGGLE_RUN_WALK)  !weapons.IS_RELOADING ? 3.4f : 1.5f)) : 0.0f;
		}
	}
	
	public float ROTATION_SPEED_MULTIPLER{
		get{
			return Input.GetKey(cInput.KEY_TOGGLE_RUN_WALK) ? 190.0f : 130.0f;
		}
	}
	
	public bool RELOAD_CONDITION{
		get{
			return Input.GetKey(cInput.KEY_RELOAD)  !weapons.CLIP_FULL || weapons.CLIP_EMPTY  !weapons.IS_ATTACKING;
		}
	}
	
	public bool ATTACK_CONDITION{
		get{
			return Input.GetKey(cInput.KEY_TRY_ATTACK);
		}
	}
	
	void Awake () { 
		gameObject.name = (networkView.isMine ? "Local" : "Remote") + "_Player";
		a.SetLayerWeight(1, 1);
		a.SetLayerWeight(2, 1);

		if(networkView.isMine){
			cInput = Utils.LoadInput();
			foreach (GameObject go in FindObjectsOfType(typeof (GameObject)))
				go.SendMessage("OnPlayerSpawn", gameObject, SendMessageOptions.DontRequireReceiver);
			moveDirection_current = transform.TransformDirection(Vector3.forward);
			
			networkView.RPC("AddOrReplaceWeapon", RPCMode.AllBuffered, (int)WeaponSystemSetup.Weapon_Slots.UNARMED, 0);
			networkView.RPC("AddOrReplaceWeapon", RPCMode.AllBuffered, (int)WeaponSystemSetup.Weapon_Slots.HANDGUN, 0);
			networkView.RPC("AddOrReplaceWeapon", RPCMode.AllBuffered, (int)WeaponSystemSetup.Weapon_Slots.RIFLE, 0);
			weapons.currentSlot = TryToSelectSlot(WeaponSystemSetup.Weapon_Slots.RIFLE);
		}
	}
	
	void Update () {
		if(networkView.isMine){
			h = Input.GetAxis("Horizontal");
			v = Input.GetAxis("Vertical");
			mouseX = Input.GetAxis("Mouse X");
			mouseY = Input.GetAxis("Mouse Y");
			
			V_LookAngle -= mouseY * mouseYSens;
			V_LookAngle = Utils.ClampAngle(V_LookAngle, -V_LookAngleThreshold, V_LookAngleThreshold);
			
			if(!IS_AIMING  !weapons.IS_SELECTION){
				Update_WeaponSelection();
			}
			
			if(weapons.lastSlot != weapons.currentSlot){
				networkView.RPC("CreateWeaponObject", RPCMode.AllBuffered, (int)weapons.currentSlot, (int)weapons.lastSlot);
				weapons.slots[(int)weapons.currentSlot].NEXT_RELOAD_EVENT = Time.fixedTime;
				weapons.lastSlot = weapons.currentSlot;
			}
			
			if(ATTACK_CONDITION){
				TryAttack();
			}
			
			if(RELOAD_CONDITION){
				TryReload();
			}
		}
		Update_Animator();
	}
	
	void LateUpdate(){
		LUpdate_CameraMovement();
		LUpdate_CalculatePlayerMovement();
		LUpdate_VLookAngle();
	}
	
	void FixedUpdate(){
		FUpdate_ApplyPlayerMovement();
	}
	
	void Update_WeaponSelection(){
		if(Input.GetAxis("Mouse ScrollWheel") > 0){
			Stop_Attack();
			Stop_Reload();
			
			weapons.currentSlot = SelectUp();
		}
		
		if(Input.GetAxis("Mouse ScrollWheel") < 0){
			Stop_Attack();
			Stop_Reload();
			
			weapons.currentSlot = SelectDown();
		}
	}
	
	
	WeaponSystemSetup.Weapon_Slots SelectUp(){
		int TEMP_currentSlot = (int)weapons.currentSlot;
		weapons.NEXT_SELECTION_EVENT = Time.fixedTime + 0.5f;
		do{
			TEMP_currentSlot++;
			if(TEMP_currentSlot == weapons.slots.Length){
				TEMP_currentSlot = 0;
				return (WeaponSystemSetup.Weapon_Slots)TEMP_currentSlot;
			}
		}
		while(!weapons.slots[TEMP_currentSlot].USED);
		return (WeaponSystemSetup.Weapon_Slots)TEMP_currentSlot;
	}
	
	WeaponSystemSetup.Weapon_Slots SelectDown(){
		int TEMP_currentSlot = (int)weapons.currentSlot;
		weapons.NEXT_SELECTION_EVENT = Time.fixedTime + 0.5f;
		do{
			if(TEMP_currentSlot == 0)
				TEMP_currentSlot = weapons.slots.Length;
			TEMP_currentSlot--;
		}
		while(!weapons.slots[TEMP_currentSlot].USED);
		return (WeaponSystemSetup.Weapon_Slots)TEMP_currentSlot;
	}
	
	void Update_Animator(){
		a.SetFloat("h", h, 0.15f, Time.deltaTime);
		a.SetFloat("v", v, 0.15f, Time.deltaTime);
		a.SetBool("aiming", networkView.isMine ? IS_AIMING : SYNC_is_aiming);
		a.SetBool("crouching", IS_CROUCHING);
		a.SetFloat("MType_Move", (networkView.isMine ? IS_AIMING : SYNC_is_aiming) ? weapons.CURRENT_WEAPON_MOTION_DATA.MOTION_TYPE_MOVEMENT_DIRECTIONAL : weapons.CURRENT_WEAPON_MOTION_DATA.MOTION_TYPE_MOVEMENT_BASE);
		a.SetFloat("MType_Attack", weapons.CURRENT_WEAPON_MOTION_DATA.MOTION_TYPE_ATTACK);
		a.SetFloat("MType_Reload", weapons.CURRENT_WEAPON_MOTION_DATA.MOTION_TYPE_RELOAD);
		a.SetFloat("bSpeed", networkView.isMine ? ANIMATOR_MOVE_SPEED : SYNC_animator_move_speed, 0.35f, Time.deltaTime);
		//a.ForceStateNormalizedTime(a.GetCurrentAnimatorStateInfo(1).normalizedTime);
	}
	
	void FUpdate_ApplyPlayerMovement(){
		if(networkView.isMine){
			if(isGrounded){
				Vector3 velocity = rigidbody.velocity;
				Vector3 velocityChange = (moveDirection_current - velocity);
					
				velocityChange.x = Mathf.Clamp(velocityChange.x, -10.0f, 10.0f);
		        velocityChange.z = Mathf.Clamp(velocityChange.z, -10.0f, 10.0f);
		        velocityChange.y = 0;
		
				rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
				
				if (Input.GetKey(cInput.KEY_JUMP)) {
		            rigidbody.velocity = new Vector3(velocity.x, Mathf.Sqrt(2 * 0.5f * (-Physics.gravity.y)), velocity.z);
		        }
			}
			rigidbody.AddForce(new Vector3 (0, Physics.gravity.y * rigidbody.mass, 0));
			isGrounded = false;
		}
	}
	
	void LUpdate_CalculatePlayerMovement(){
		aimingWeightControl = Mathf.MoveTowards(aimingWeightControl, (networkView.isMine ? IS_AIMING : SYNC_is_aiming) ? 1.0f : 0.0f, Time.deltaTime * 5.0f);
		
		if(networkView.isMine){
			cameraForward = c.transform.TransformDirection(Vector3.forward);
			cameraForward.y = 0;
			
			rotateDirection_target = h * new Vector3(cameraForward.z, 0, -cameraForward.x) + v * cameraForward;
			
			Vector3 TEMP_rotateDirection_normal = Vector3.RotateTowards(rotateDirection_current, rotateDirection_target + transform.forward, ROTATION_SPEED_MULTIPLER * Mathf.Deg2Rad * Time.deltaTime, 1000.0f);
			Vector3 TEMP_rotateDirection_facing = cameraForward;
			rotateDirection_current = Vector3.Slerp(TEMP_rotateDirection_normal, TEMP_rotateDirection_facing, aimingWeightControl);
			rotateDirection_current.Normalize();
			
			if(rotateDirection_current != Vector3.zero)
				transform.rotation = Quaternion.LookRotation(rotateDirection_current);
			
			Vector3 TEMP_moveDirection_facing = transform.TransformDirection(new Vector3(h, 0, v));
			TEMP_moveDirection_facing.Normalize();
			
			moveDirection_current = IS_AIMING ? TEMP_moveDirection_facing : rotateDirection_current;
			
			moveDirection_current *= VELOCITY_MULTIPLER;	
			moveDirection_current.y = 0.0f;
		}
	}
	
	void LUpdate_CameraMovement(){
		if(networkView.isMine){
			if(c.canControl){
				c.x += mouseX * mouseXSens;
		  	  	c.y -= mouseY * mouseYSens;
		 		
		   		c.y = V_LookAngle;
				
				c.cameraRotationPoint_current = Vector3.MoveTowards(c.cameraRotationPoint_current, IS_AIMING ? (IS_CROUCHING ? c.cameraRotationPoint_crouching_aiming : c.cameraRotationPoint_aiming) : c.cameraRotationPoint_normal, Time.deltaTime * c.speed_offsetChange);
				c.cameraPositionOffset_current = Vector3.MoveTowards(c.cameraPositionOffset_current, IS_AIMING ? (IS_CROUCHING ? c.camera_PositionOffset_crouching_aiming : c.camera_PositionOffset_aiming) : c.camera_PositionOffset_normal, Time.deltaTime * c.speed_offsetChange);
				
				c.cRotation = Quaternion.Euler(c.y, c.x, 0);
		        c.cPosition = c.CalculateCameraPosition(c.cameraRotationPoint_current, c.cameraPositionOffset_current, c.cRotation);
				c.transform.position = c.cPosition;
				c.transform.rotation = c.cRotation;
			}
		}
	}
	
	void LUpdate_VLookAngle(){
		foreach(Transform segment in upperBodyChain){
			segment.RotateAround(segment.position, transform.right, (V_LookAngle * aimingWeightControl) / upperBodyChain.Length);
		}
	}
	
	void TryAttack(){
		GTA_Weapons.Weapon_Attack_Type TEMP_attack_type = weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.ATTACK_TYPE;
		switch(TEMP_attack_type){
			case(GTA_Weapons.Weapon_Attack_Type.MELEE):
				break;
			case(GTA_Weapons.Weapon_Attack_Type.RAYCAST):
				if (aimingWeightControl > 0.95f  !weapons.CLIP_EMPTY  !weapons.IS_ATTACKING){
					//networkView.RPC("NetworkEvent_AttackStart", RPCMode.All, "OnAttackStart");
					networkView.RPC("NetworkSetAnimatorBool", RPCMode.All, "attack", true);
					CastRay();
					weapons.slots[(int)weapons.currentSlot].AMMO_IN_CLIP--;
					weapons.slots[(int)weapons.currentSlot].NEXT_ATTACK_EVENT = Time.fixedTime + weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.ATTACK_RATE;
					StartCoroutine("Delayed_AttackFinish", weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.ATTACK_RATE - 0.05f);
				}
				break;
			case(GTA_Weapons.Weapon_Attack_Type.PROJECTILE):
				if (aimingWeightControl > 0.95f  !weapons.CLIP_EMPTY  !weapons.IS_ATTACKING){
					networkView.RPC("NetworkEvent_AttackStart", RPCMode.All, "OnAttackStart");
					networkView.RPC("NetworkSetAnimatorBool", RPCMode.All, "attack", true);
					weapons.slots[(int)weapons.currentSlot].WEAPON_OBJECT.EjectTest();
					weapons.slots[(int)weapons.currentSlot].AMMO_IN_CLIP--;
					weapons.slots[(int)weapons.currentSlot].NEXT_ATTACK_EVENT = Time.fixedTime + weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.ATTACK_RATE;
					StartCoroutine("Delayed_AttackFinish", weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.ATTACK_RATE - 0.05f);
				}
				break;
		}
	}
	
	void TryReload(){
		GTA_Weapons.Weapon_Attack_Type TEMP_attack_type = weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.ATTACK_TYPE;
		if(TEMP_attack_type != GTA_Weapons.Weapon_Attack_Type.MELEE){
			if(!weapons.IS_SELECTION  weapons.HAS_AMMO  !weapons.IS_RELOADING){
				networkView.RPC("NetworkEvent_ReloadStart", RPCMode.All, "OnReloadStart");
				networkView.RPC("NetworkSetAnimatorBool", RPCMode.All, "reload", true);
				weapons.slots[(int)weapons.currentSlot].NEXT_RELOAD_EVENT = Time.fixedTime + weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.RELOAD_RATE;
				StartCoroutine("Delayed_ReloadFinish", weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.RELOAD_RATE - 0.1f);
			}
		}
	}
	
	void CastRay(){
		float TEMP_inaccuracy = weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.INACCURACY;
		RaycastHit hit;
		Ray ray = Camera.main.ViewportPointToRay (new Vector3(0.5f + Utils.RandomValue(TEMP_inaccuracy), 0.5f + Utils.RandomValue(TEMP_inaccuracy), 0.0f));
		if(Physics.Raycast(ray, out hit, 1000.0f, weapons.hitLayers)){
			networkView.RPC("Sync_Hit_Effect", RPCMode.All, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal), hit.collider.tag);
			networkView.RPC("NetworkEvent_AttackStart", RPCMode.All, hit.point, hit.distance);

			DecalGen(hit.point, hit.normal, hit.collider); 
		}
		else{
			Vector3 imaginary_hit_point = ray.GetPoint(50.0f);
			networkView.RPC("NetworkEvent_AttackStart", RPCMode.All, imaginary_hit_point, 50.0f);
		}
	}

	GameObject DecalGen(Vector3 p, Vector3 n, Collider c){
      	GameObject decalInst;
   
      	decalInst = (GameObject)Instantiate(decalPrefab, p, Quaternion.FromToRotation(Vector3.up, n));
   
      	MeshFilter mf = decalInst.GetComponent(typeof(MeshFilter)) as MeshFilter;
      	Mesh m = mf.mesh;
   
     	Vector3[] verts = m.vertices;
   
      	for (int i = 0; i < verts.Length; i++){
         	verts[i] = decalInst.transform.TransformPoint(verts[i]);
    
         	if (verts[i].x > c.bounds.max.x)
            	verts[i].x = c.bounds.max.x;
    
         	if (verts[i].x < c.bounds.min.x)
            	verts[i].x = c.bounds.min.x;
    
	     	if (verts[i].y > c.bounds.max.y)
		        verts[i].y = c.bounds.max.y;
		
	     	if (verts[i].y < c.bounds.min.y)
	       	 	verts[i].y = c.bounds.min.y;
	
	     	if (verts[i].z > c.bounds.max.z)
	        	verts[i].z = c.bounds.max.z;
	
	     	if (verts[i].z < c.bounds.min.z)
	        	verts[i].z = c.bounds.min.z;
         	verts[i] = decalInst.transform.InverseTransformPoint(verts[i]);
         	m.vertices = verts;
      }
	  Destroy(decalInst, 5.0f);
      return decalInst;
   } 
	
	
	WeaponSystemSetup.Weapon_Slots TryToSelectSlot(WeaponSystemSetup.Weapon_Slots slot){
		Stop_Attack();
		Stop_Reload();
		
		WeaponSystemSetup.Weapon_Slots TEMP_weapon_slot = weapons.slots[(int)slot].USED ? slot : WeaponSystemSetup.Weapon_Slots.UNARMED;
		weapons.slots[(int)TEMP_weapon_slot].NEXT_RELOAD_EVENT = Time.fixedTime;
		return TEMP_weapon_slot;
	}
	
	[RPC]
	void NetworkEvent_ReloadStart(string method_name){
		if(weapons.slots[(int)weapons.currentSlot].HAS_WEAPON_OBJECT)
			weapons.slots[(int)weapons.currentSlot].WEAPON_OBJECT.Invoke(method_name, 0.0f);
	}
	
	[RPC]
	void NetworkEvent_AttackStart(Vector3 hit_point, float hit_distance){
		if(weapons.slots[(int)weapons.currentSlot].HAS_WEAPON_OBJECT)
			weapons.slots[(int)weapons.currentSlot].WEAPON_OBJECT.OnAttackStart(hit_point, hit_distance);
	}
	
	[RPC]
	void NetworkEvent_ReloadEnd(string method_name){
		if(weapons.slots[(int)weapons.currentSlot].HAS_WEAPON_OBJECT)
			weapons.slots[(int)weapons.currentSlot].WEAPON_OBJECT.Invoke(method_name, 0.0f);
	}
	
	[RPC]
	void NetworkSetAnimatorBool(string propertyName, bool val){
		a.SetBool(propertyName, val);
	}
	
	[RPC]
	void Sync_Hit_Effect(Vector3 pos, Quaternion rot, string hit_tag){
		foreach(WeaponSystemSetup.Hit_Event hit_event in weapons.hitEvents){
			if(hit_event.hitTag == hit_tag){
				hit_event.root.position = pos;
				hit_event.root.rotation = rot;
				foreach(ParticleEmitter emitter in hit_event.emittOnHit)
					emitter.Emit();
				break;
			}
		}
	}
	
	[RPC]
	void AddOrReplaceWeapon(int slot, int ID){
		weapons.slots[slot].ID = ID;
		weapons.slots[slot].USED = true;
		weapons.slots[slot].AMMO_IN_CLIP = (int)weapons.slots[slot].WEAPON_INFO.CLIP_SIZE;
		weapons.slots[slot].HAS_AMMO = (int)weapons.slots[slot].WEAPON_INFO.MAX_AMMO;
	}
	
	[RPC]
	void CreateWeaponObject(int slot_current, int slot_last){
		if(weapons.slots[slot_last].HAS_WEAPON_OBJECT)
			Destroy(weapons.slots[slot_last].WEAPON_OBJECT.gameObject);
		if(weapons.slots[slot_current].WEAPON_INFO.OBJECT){
			Vector3 TEMP_pos = weapons.weaponBone.position;
			Quaternion TEMP_rot = weapons.weaponBone.rotation;
			GameObject TEMP_weapon_object = Instantiate(weapons.slots[slot_current].WEAPON_INFO.OBJECT, TEMP_pos, TEMP_rot) as GameObject;
			TEMP_weapon_object.transform.parent = weapons.weaponBone;
			TEMP_weapon_object.transform.localPosition = Vector3.zero;
			TEMP_weapon_object.transform.localRotation = Quaternion.identity;
			weapons.slots[slot_current].WEAPON_OBJECT = TEMP_weapon_object.GetComponent<GTA_WeaponManager>();
			weapons.slots[slot_current].WEAPON_OBJECT.owner = gameObject.GetComponent<GTA_Controller>();
		}
	}
	
	void Stop_Attack(){
		StopCoroutine("Delayed_AttackFinish");
		networkView.RPC("NetworkSetAnimatorBool", RPCMode.All, "attack", false);
	}
	
	void Stop_Reload(){
		StopCoroutine("Delayed_ReloadFinish");
		networkView.RPC("NetworkSetAnimatorBool", RPCMode.All, "reload", false);
	}
	
	IEnumerator Delayed_AttackFinish(float time) {
		yield return new WaitForSeconds(time);
		networkView.RPC("NetworkSetAnimatorBool", RPCMode.All, "attack", false);
	}
	
	IEnumerator Delayed_ReloadFinish(float time) {
		yield return new WaitForSeconds(time);
		
		int TEMP_need_ammo = (int)weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.CLIP_SIZE - weapons.slots[(int)weapons.currentSlot].AMMO_IN_CLIP;
		if(weapons.slots[(int)weapons.currentSlot].HAS_AMMO >= TEMP_need_ammo){
			weapons.slots[(int)weapons.currentSlot].HAS_AMMO -= TEMP_need_ammo;
			weapons.slots[(int)weapons.currentSlot].AMMO_IN_CLIP = (int)weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.CLIP_SIZE;
		}
		else{
			weapons.slots[(int)weapons.currentSlot].AMMO_IN_CLIP = weapons.slots[(int)weapons.currentSlot].HAS_AMMO;
			weapons.slots[(int)weapons.currentSlot].HAS_AMMO = 0;
		}
		networkView.RPC("NetworkEvent_ReloadEnd", RPCMode.All, "OnReloadFinish");
		yield return new WaitForSeconds(0.1f);
		networkView.RPC("NetworkSetAnimatorBool", RPCMode.All, "reload", false);
    }
	
	void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
		Vector3 TEMP_pos = Vector3.zero;
		Quaternion TEMP_rot = Quaternion.identity;
		float TEMP_V_LookAngle = 0.0f;
		//float TEMP_aimingWeightControl = 0.0f;
		float TEMP_h = 0.0f;
	 	float TEMP_v = 0.0f; 
		int TEMP_current_weapon_slot = 0;
		int TEMP_last_weapon_slot = 0;
		
		bool TEMP_SYNC_is_aiming = false;
		//bool TEMP_SYNC_is_attacking = false;
		//bool TEMP_SYNC_is_reloading = false;
		float TEMP_SYNC_animator_move_speed = 0.0f;
		
		
		if (stream.isWriting) {
            TEMP_pos = transform.position;
			TEMP_rot = transform.rotation;
			TEMP_V_LookAngle = V_LookAngle;
			//TEMP_aimingWeightControl = aimingWeightControl;
			TEMP_h = h;
	 		TEMP_v = v; 
			TEMP_current_weapon_slot = (int)weapons.currentSlot;
			TEMP_last_weapon_slot = (int)weapons.lastSlot;
			
			TEMP_SYNC_is_aiming = IS_AIMING;
			//TEMP_SYNC_is_attacking = weapons.IS_ATTACKING  !weapons.IS_SELECTION;
			//TEMP_SYNC_is_reloading = weapons.IS_RELOADING  !weapons.IS_SELECTION;
			TEMP_SYNC_animator_move_speed = ANIMATOR_MOVE_SPEED;
			
			stream.Serialize(ref TEMP_pos);
			stream.Serialize(ref TEMP_rot);
			stream.Serialize(ref TEMP_V_LookAngle);
			//stream.Serialize(ref TEMP_aimingWeightControl);
			stream.Serialize(ref TEMP_h);
			stream.Serialize(ref TEMP_v);
			stream.Serialize(ref TEMP_current_weapon_slot);
			stream.Serialize(ref TEMP_last_weapon_slot);
			
			stream.Serialize(ref TEMP_SYNC_is_aiming);
			//stream.Serialize(ref TEMP_SYNC_is_attacking);
			//stream.Serialize(ref TEMP_SYNC_is_reloading);
			stream.Serialize(ref TEMP_SYNC_animator_move_speed);
        } else {
           	stream.Serialize(ref TEMP_pos);
			stream.Serialize(ref TEMP_rot);
			stream.Serialize(ref TEMP_V_LookAngle);
			//stream.Serialize(ref TEMP_aimingWeightControl);
			stream.Serialize(ref TEMP_h);
			stream.Serialize(ref TEMP_v);
			stream.Serialize(ref TEMP_current_weapon_slot);
			stream.Serialize(ref TEMP_last_weapon_slot);
			
			stream.Serialize(ref TEMP_SYNC_is_aiming);
			//stream.Serialize(ref TEMP_SYNC_is_attacking);
			//stream.Serialize(ref TEMP_SYNC_is_reloading);
			stream.Serialize(ref TEMP_SYNC_animator_move_speed);
			
			transform.position = TEMP_pos;
			transform.rotation = TEMP_rot;
			V_LookAngle = TEMP_V_LookAngle;
			//aimingWeightControl = TEMP_aimingWeightControl;
			h = TEMP_h;
	 		v = TEMP_v; 
			weapons.currentSlot = (WeaponSystemSetup.Weapon_Slots)TEMP_current_weapon_slot;
			weapons.currentSlot = (WeaponSystemSetup.Weapon_Slots)TEMP_last_weapon_slot;
       		
			SYNC_is_aiming = TEMP_SYNC_is_aiming;
			//SYNC_is_attacking = TEMP_SYNC_is_attacking;
			//SYNC_is_reloading = TEMP_SYNC_is_reloading;
			SYNC_animator_move_speed = TEMP_SYNC_animator_move_speed;
		}
    }
	
	void OnGUI(){
		if(networkView.isMine){
			if(SHOW_DEBUG_INFO){
				GUI.Label(new Rect(10, 10, 200, 20), "is selection: " + weapons.IS_SELECTION.ToString());
				GUI.Label(new Rect(10, 30, 200, 20), "is reloading: " + weapons.IS_RELOADING.ToString());
				GUI.Label(new Rect(10, 50, 200, 20), "is attacking: " + weapons.IS_ATTACKING.ToString());
				GUI.Label(new Rect(10, 70, 200, 20), "is crouching: " + IS_CROUCHING.ToString());
				GUI.Label(new Rect(10, 90, 200, 20), "is aiming: " + IS_AIMING.ToString());
				GUI.Label(new Rect(10, 110, 200, 20), "is moving: " + IS_MOVING.ToString());
				GUI.Label(new Rect(10, 130, 200, 20), "can aim: " + weapons.CAN_AIM.ToString());
				GUI.Label(new Rect(10, 150, 200, 20), "look angle: " + V_LookAngle.ToString("f2"));
				GUI.Label(new Rect(10, 170, 200, 20), "mouseX: " + mouseX.ToString());
				GUI.Label(new Rect(10, 190, 200, 20), "mouseY: " + mouseX.ToString());
			}
			
			if(weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.ATTACK_TYPE != GTA_Weapons.Weapon_Attack_Type.MELEE){
				string TEMP_text_ammo_in_clip = weapons.slots[(int)weapons.currentSlot].AMMO_IN_CLIP.ToString();
				string TEMP_text_has_ammo = weapons.slots[(int)weapons.currentSlot].HAS_AMMO.ToString();
				
				
				Utils.DrawOutlineText(new Rect(Screen.width - 160, 50, 40, 30), TEMP_text_ammo_in_clip, UIStyle, Color.black, Color.grey, 4.0f);
				Utils.DrawOutlineText(new Rect(Screen.width - 240, 50, 85, 30), TEMP_text_has_ammo, UIStyle, Color.black, Color.white, 4.0f);
			}
			GUI.DrawTexture(new Rect(Screen.width - 250, 50, 130, 130), weapons.slots[(int)weapons.currentSlot].WEAPON_INFO.ICON, ScaleMode.ScaleToFit, true, 0f);
		}
	}
	
	void OnCollisionStay () {
	    isGrounded = true;    
	}
}