private void Update() { Vector3 mousePos = cam.ScreenToWorldPoint(Input.mousePosition); mousePos.z = 0f; transform.rotation = Quaternion.Euler(Vector3.forward * CustomLib.AngleFromPos(transform.position, mousePos)); float dist = Vector2.Distance(transform.position, mousePos); if (dist <= 2f) { dist = 0f; } else if (dist >= 10) { dist = Const.MaxAngle; } else { dist = Const.Dist2AngleA * dist + Const.Dist2AngleB; } coeff = 2 * Mathf.PI / (2 * Mathf.PI - dist); // 1-8 angle = 360f - (dist * Mathf.Rad2Deg); if (musicEffect != null) { musicEffect.UpdateShape(angle); } ApplyMusic(angle); }
public void ApplyMusic(float angle) { currentDebuff = new Ablaze(force * Time.deltaTime * coeff); foreach (GameObject coreling in fieldOfView.seenCorelings) { Demon demon = coreling.GetComponent <Demon>(); if (demon != null) { float angleFromMus = CustomLib.AngleFromPos(transform.position, demon.transform.position) - transform.rotation.eulerAngles.z; if (angleFromMus <= angle / 2f && angleFromMus >= -angle / 2f) { demon.AddDebuff(currentDebuff); } } } }
private void OnCollisionEnter2D(Collision2D collision) { if (!(collision.collider.CompareTag(gameObject.tag))) // human collides with demon { return; } Unit unit = collision.collider.GetComponent <Unit>(); if (unit == null) // Error { Debug.LogError("An object tagged as " + collision.collider.tag + " misses the unit script : " + collision.collider.name); return; } if (unit.aiPathfinder == null) // Error { Debug.LogError("A unit misses the AIPathfinder component : " + collision.collider.name); return; } float targetDir = CustomLib.AngleFromPos(Vector2.zero, unit.aiPathfinder.direction); float sourceDir = CustomLib.AngleFromPos(Vector2.zero, aiPathfinder.direction); // Get angles from directions and positions float angleTarget; float angleSource; if (unit.aiPathfinder.direction.Equals(Vector2.zero)) { angleTarget = 0f; } else { angleTarget = CustomLib.AngleFromPos(unit.rb.position, rb.position) - targetDir; if (angleTarget <= -180) // Transform angles from 0;360 to -180;180 { angleTarget += 360; } else if (angleTarget > 180) { angleTarget -= 360; } } if (aiPathfinder.direction.Equals(Vector2.zero)) { angleSource = 0f; } else { angleSource = CustomLib.AngleFromPos(rb.position, unit.rb.position) - sourceDir; if (angleSource <= -180) { angleSource += 360; } else if (angleSource > 180) { angleSource -= 360; } } if (Mathf.Abs(angleTarget) > Mathf.Abs(angleSource)) // The unit with the larger angle deal with the collision. The other awaits orders { return; } float angleIntensity = Mathf.Abs(angleSource); float dir = angleSource / angleIntensity; // 1 if left ; -1 if right float newDir = sourceDir - dir * (90f - angleIntensity); GetAway(Mathf.Deg2Rad * newDir); // source unit.GetAway(Mathf.Deg2Rad * (newDir + 180f)); // target }