void InstallForceField(Forcefield _temp) { _temp.CollisionEnter = CollisionEnter; _temp.CollisionExit = CollisionExit; _temp.CollisionStay = CollisionStay; _temp.DecaySpeed = DecaySpeed; _temp.ReactSpeed = ReactSpeed; }
// INITIALIZATION void Start() { if (FixRigidbody && Field.Length > 0) { // MeshCollider[] meshColliders = this.gameObject.GetComponentsInChildren<MeshCollider>(); GameObject newGameObject = new GameObject(); newGameObject.name = this.gameObject.name + "[MeshCollidersFixer]"; newGameObject.transform.parent = null; newGameObject.transform.position = this.transform.position; newGameObject.transform.rotation = this.transform.rotation; MeshColliderChild colliderFixer = newGameObject.gameObject.AddComponent <MeshColliderChild>(); colliderFixer.Install(this.transform); // List<Material> materials = new List<Material>(); for (int i = 0; i < Field.Length; i++) { if (Field[i] != null) { if (Field[i].GetComponent <Forcefield>() == null) { Forcefield _field = Field[i].AddComponent <Forcefield>(); InstallForceField(_field); } Field[i].transform.parent = newGameObject.transform; MeshCollider _collider = Field[i].GetComponent <MeshCollider>(); _collider.enabled = true; } } } // Cache required components if (this.gameObject.GetComponent <Renderer>() != null) { mat = this.gameObject.GetComponent <Renderer>().materials; } mesh = this.gameObject.GetComponent <MeshFilter>(); // Generate unique IDs for optimised performance // since script has to access them each frame shaderPosID = new int[interpolators]; shaderPowID = new int[interpolators]; for (int i = 0; i < interpolators; i++) { shaderPosID[i] = Shader.PropertyToID("_Pos_" + i.ToString()); shaderPowID[i] = Shader.PropertyToID("_Pow_" + i.ToString()); } // Initialize data array shaderPos = new Vector4[interpolators]; }
void OnMouse() { if (Input.GetMouseButtonDown(0)) { if (BallPrefab != null) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); GameObject prefab = (GameObject)GameObject.Instantiate(BallPrefab, transform.position + transform.forward, Quaternion.identity); prefab.GetComponent <Rigidbody>().AddForce(ray.direction * 55f, ForceMode.VelocityChange); prefab.GetComponent <Rigidbody>().AddTorque(new Vector3(Random.Range(-20f, 20f), Random.Range(-20f, 20f), Random.Range(-20f, 20f))); } } if (Input.GetMouseButton(1) && curTime >= FireRate) { curTime = 0.0f; RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 500.0f)) { Forcefield ffHit = hit.transform.gameObject.GetComponent <Forcefield>(); if (ffHit != null) { ffHit.OnHit(hit.point, hitPower); if (ParticlePrefab != null) { GameObject.Instantiate(ParticlePrefab, hit.point, Quaternion.identity); } if (ShieldHitSFX.Length > 0) { int index = Random.Range(0, ShieldHitSFX.Length); for (int i = 0; i < 10; i++) { if (!ShieldHitSFX[index].isPlaying) { break; } else { index = Random.Range(0, ShieldHitSFX.Length); } } ShieldHitSFX[index].transform.position = hit.point; ShieldHitSFX[index].Play(); } } } } }
// Update is called once per frame void Update() { // Activate on Left mouse button if (F3DWeaponRangeController.isObserverActive && Input.GetMouseButton(0)) { // Returns a ray going from camera through a screen point RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Casts a ray against colliders in the scene if (Physics.Raycast(ray, out hit, 500.0f)) { // Get Forcefield script component Forcefield ffHit = hit.transform.GetComponentInParent <Forcefield>(); // Generate random hit power value and call Force Field script if successful if (ffHit != null) { float hitPower = Random.Range(-2f, 2f); ffHit.OnHit(hit.point, hitPower); ReactSpeed = ffHit.ReactSpeed; if (curTime >= ReactSpeed) { Instantiate(ShieldFlashLight, hit.point + hit.normal, Quaternion.identity); Instantiate(ShieldHit, hit.point + hit.normal * 0.5f, Quaternion.identity); if (ShieldHitFX != null) { AudioSource temp = ((Transform)Instantiate(ShieldHitFX[Random.Range(0, ShieldHitFX.Length)], hit.point, Quaternion.identity)).GetComponent <AudioSource>(); temp.volume -= Random.Range(0f, 0.25f); temp.pitch -= Random.Range(-0.03f, 0.03f); temp.Play(); } curTime = 0f; } } } } curTime += Time.deltaTime; }
// Update is called once per frame void Update() { // Activate on Left mouse button if (Input.GetMouseButton(0)) { // Returns a ray going from camera through a screen point RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Casts a ray against colliders in the scene if (Physics.Raycast(ray, out hit, 500.0f)) { // Get Forcefield script component Forcefield ffHit = hit.transform.GetComponentInParent <Forcefield>(); // Generate random hit power value and call Force Field script if successful if (ffHit != null) { float hitPower = Random.Range(-2f, 2f); ffHit.OnHit(hit.point, hitPower); } } } }