- Home /
Spider-man Web slinging behavior 2D platformer
Hello, I am currently trying to implement a web slinging behavior in my script. With the code that I currently have, the web-line sprite is being instantiated for the desired length but doesn't get deleted though I call the DestroyWebzipCo() coroutine function and the console outputs "deleting webs". Also, there is no delay when instantiating each prefab, thus (i think) the WaitForSeconds() does not run for some reason, but the instantiation runs, which is also in my coroutine. Note: I know my code is really messy and inefficient but I am just looking for a way to make it work. Also Note that function Shoot() works and is not part of the websling function. Any help would be greatly appreciated. Also welcome any suggestions in making my code shorter and more user friendly. My code: using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; public class Weapon : MonoBehaviour { public Transform firePoint; public GameObject bulletPrefab; public GameObject weblinePrefab; public GameObject weblineEnd; public CharacterController2D player; public float webRange = 5f; public float fireRate = 0.2f; private float pressTime = 0f; // Update is called once per frame void Update () { if (Input.GetButtonDown ("Fire1") && fireRate >= 0.2f) { Shoot (); //pressTime += Time.deltaTime; fireRate = 0f; } else { //pressTime = 0f; fireRate += Time.deltaTime; } if (Input.GetKeyDown(KeyCode.L)) { Webzip(); } Debug.DrawRay(firePoint.position, new Vector3(webRange, 0, 0), Color.green); } void Shoot() { Instantiate (bulletPrefab, firePoint.position, firePoint.rotation); } void Webzip() { RaycastHit2D fireRay = Physics2D.Raycast (firePoint.position, Vector2.right, webRange); float weblinelength = weblinePrefab.GetComponent<SpriteRenderer>().bounds.max.x; if (fireRay.collider != null) { Debug.Log ("hit something!"); for (int i = 0; i <= (fireRay.distance/weblinelength); i++) { if (Input.GetKeyUp (KeyCode.L)) { StartCoroutine(DestroyWebzipCo(false)); return; } if (i == (fireRay.distance/weblinelength)) { Instantiate (weblineEnd, firePoint.position + i * new Vector3(weblinelength, 0,0), weblineEnd.transform.rotation); } else { StartCoroutine(WebzipCo(i)); } } StartCoroutine(DestroyWebzipCo(true)); } else { Debug.Log ("Didn't hit something!"); for (int i = 0; i <= (new Vector2(webRange, 0).magnitude/weblinelength); i++) { if (Input.GetKeyUp (KeyCode.L)) { StartCoroutine(DestroyWebzipCo(false)); return; } if (i == (new Vector2 (webRange, 0).magnitude / weblinelength)) { Instantiate (weblineEnd, firePoint.position + i * new Vector3 (weblinePrefab.GetComponent<SpriteRenderer> ().bounds.max.x, 0, 0), weblineEnd.transform.rotation); return; } else { StartCoroutine(WebzipCo(i)); } } StartCoroutine(DestroyWebzipCo(false)); } } IEnumerator WebzipCo(float i) { yield return new WaitForSecondsRealtime (0.5f); Instantiate (weblinePrefab, firePoint.position + i * new Vector3(weblinePrefab.GetComponent<SpriteRenderer>().bounds.max.x, 0,0), weblinePrefab.transform.rotation); } IEnumerator DestroyWebzipCo(bool hit) { Debug.Log ("Deleting webs"); GameObject[] weblines = GameObject.FindGameObjectsWithTag ("web-line"); weblines.OrderByDescending (g => g.transform.position.x); for (int i = 0; i < weblines.Count(); i++) { yield return new WaitForSecondsRealtime (0.5f); if (hit) { player.transform.position = new Vector2 (Mathf.Lerp (transform.position.x, weblines[weblines.Count()-i].transform.position.x, 0.5f), Mathf.Lerp (transform.position.y, weblines[weblines.Count()-i].transform.position.y, 0.5f)); } Destroy (weblines[i].gameObject); } } }