private void Start() { seeker = GetComponent <Seeker>(); rb = GetComponent <Rigidbody2D>(); grappableEntity = GetComponent <GrappableEntity>(); spriteRenderer = GetComponentInChildren <SpriteRenderer>(); InvokeRepeating("UpdatePath", 0f, 0.5f); defaultPosition = transform.position; }
GrappableEntity GetClosestGrappable() { Vector3 pos = this.gameObject.transform.position; float dist = float.PositiveInfinity; GrappableEntity target = null; foreach (GrappableEntity go in GrappableEntity.Entities) { if ((go.gameObject.transform.position - pos).sqrMagnitude < dist) { dist = (go.gameObject.transform.position - pos).sqrMagnitude; target = go; } } return(target); }
// Update is called once per frame void Update() { //ANIMATION CONTROL if (!controller.isGrounded()) { animator.SetBool("Jump", true); } else { animator.SetBool("Jump", false); } animator.SetFloat("Speed", Mathf.Abs(horizontalInput)); horizontalInput = Input.GetAxisRaw("Horizontal") * speed; if (Input.GetButtonDown("Jump")) { jump = true; animator.SetBool("Jump", true); } // BEHAVIOR // keydown event. keeps reads and writes low if (Input.GetKeyDown(KeyCode.LeftShift)) { grappable = GetClosestGrappable(); if (grappable != null) { if (Vector2.Distance(transform.position, grappable.transform.position) < RADIUS) { beamRenderer.enabled = true; grappable.SetGrappled(true); isGrappled = true; } } } if (Input.GetKey(KeyCode.LeftShift) && grappable != null && isGrappled) { rb.velocity = new Vector2(0f, 0f); rb.gravityScale = 0; beamRenderer.SetPosition(0, beampoint.position); beamRenderer.SetPosition(1, grappable.gameObject.transform.position); } if (Input.GetKeyUp(KeyCode.LeftShift)) { beamRenderer.enabled = false; rb.gravityScale = 2.5f; isGrappled = false; this.gameObject.transform.rotation = Quaternion.identity; grappable.SetGrappled(false); } }