private bool Attack(Vector2 direction, float attackPower, float distance, float knockback) { float x = direction.x; float y = direction.y; if (Mathf.Abs(x) > Mathf.Abs(y)) { direction.x = Mathf.Sign(x); direction.y = 0; } else { direction.y = Mathf.Sign(y); direction.x = 0; } Vector2 position = this.gameObject.transform.position; RaycastHit2D hit = Physics2D.CircleCast(position, 1, direction, distance, mask); Debug.DrawRay(position, direction, Color.blue, 1); CharacterHealthComponent health = null; Rigidbody2D body = null; try { health = hit.collider.gameObject.GetComponent <CharacterHealthComponent>(); } catch { //add debug code here if needed } if (hit && health != null) { health.Damage(Mathf.CeilToInt(attackPower)); body = health.gameObject.GetComponent <Rigidbody2D>(); if (body != null && knockback > 0) { body.AddForce(direction * knockback, ForceMode2D.Impulse); } return(true); } return(false); }
private void OnCollisionEnter2D(Collision2D collision) { if (((1 << collision.gameObject.layer) & mask) != 0)//bitmask shit, do not touch { //Debug.Log(true); CharacterHealthComponent health = collision.gameObject.GetComponent <CharacterHealthComponent>(); health.Damage(damage); if (knockback > 0) { Rigidbody2D otherBody = collision.gameObject.GetComponent <Rigidbody2D>(); if (otherBody) { otherBody.AddForce(direction * knockback); } } } Destroy(this.gameObject); }
private float rootFlip; //the x scale of the root when activated void Update() { float returnSpdMod = 1f; if (returning) //if returning { returnSpdMod = 0.5f; //make return speed half of normal speed destination = transform.position; //constantly update the return destination with the object world position } //move end point of tentacle if ((tentaclePos - destination).magnitude <= speed * returnSpdMod * Time.deltaTime) //check if tentacle is within tolerance of destination { if (!returning) //if attacking outward (not returning) { tentaclePos = destination; //set end point to the destination destination = transform.position; //set new destination to the start point returning = true; //set returning to true (now it is returning) } else { tentaclePos = destination; //set end point to the destination returning = false; //no longer returning (because it has returned to the start point) this.gameObject.SetActive(false); //deactivate tentacle } } else { tentaclePos += (destination - tentaclePos).normalized * speed * returnSpdMod * Time.deltaTime; //move tentacle towards destination } //apply changes to tentacle tentacleTile.size = new Vector2(1, (tentaclePos - new Vector2(transform.position.x, transform.position.y)).magnitude); //THE FOLLOWING CODE WORKS BUT I HAVE NO F*****G IDEA HOW, A RESULT OF PAINFUL TRIAL AND ERROR Quaternion tentacleRot = Quaternion.LookRotation((new Vector3(tentaclePos.x, tentaclePos.y, 0f) - transform.position).normalized, Vector3.forward); transform.rotation = Quaternion.Euler(0f, 0f, (tentacleRot.eulerAngles.x + 90f) * rootFlip); //DO NOT TOUCH THE TWO LINES ABOVE, IT IS BLACK MAGIC tentacleEnd.transform.position = tentaclePos; //apply changes to end point of the tentacle //damage raycast if (!returning) //if the tentacle is attacking outward { RaycastHit2D hit = Physics2D.Linecast(transform.position, tentaclePos, 1 << 9); //linecast between start point and end point (layer masked to player layer) if (hit) //if the linecast hit something { //-- barrowing code from CharacterMeleeComponent -- CharacterHealthComponent health = null; Rigidbody2D body = null; try { health = hit.collider.gameObject.GetComponent <CharacterHealthComponent>(); //the player's health from the collider } catch { /*nothing*/ } if (hit && health != null) { health.Damage(damage); body = health.gameObject.GetComponent <Rigidbody2D>(); if (body != null && knockback > 0) { body.AddForce((destination - tentaclePos).normalized * knockback, ForceMode2D.Impulse); } } // -- -- //return tentacle destination = transform.position; //set new destination to the start point returning = true; //set returning to true } } }