void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Enemy") { Vector3 direction; if (dash) { direction = this.transform.position - playerPos; } else if (whirlwind) { direction = other.transform.position - playerPos; } else { direction = other.transform.position - playerPos; controlsScript.HitSuccess(); // if it hits an enemy, it tells the player controls script. attackDamage = defaultAttackDamage; } //Initialises a camera shake coroutine when hitting an enemy. if (TekituCameraShaker.Instance != null) { TekituShakeParameters shakeSettings = new TekituShakeParameters(shakeDuration, direction, 0.5f, shakeStrength); StartCoroutine(TekituCameraShaker.Instance.Shake(shakeSettings)); } other.gameObject.GetComponent <GenericEnemyScript>().Hit(attackDamage, direction, pushStrength); } }
//Shakes the camera in the direction of dir relative to the centre of the screen. This should usually be the direction of attack //to simulate the attack momentum shaking the screen. If a general shake is needed, pass dir = Vector3.zero. public IEnumerator Shake(TekituShakeParameters shakeparam) { //TODO: roughness? waitforseconds(roughness*time.deltatime ???) Vector3 originalPos = transform.localPosition; //Normalises vector so we can use it for shake direction. if (shakeparam.dir.magnitude > 1) { shakeparam.dir.Normalize(); } else if (shakeparam.dir.magnitude < 1 && shakeparam.dir.magnitude > 0) { shakeparam.dir = new Vector3(shakeparam.dir.x / shakeparam.dir.magnitude, shakeparam.dir.y / shakeparam.dir.magnitude, shakeparam.dir.z / shakeparam.dir.magnitude); } float timeElapsed = 0; while (timeElapsed <= shakeparam.duration) { yield return(null); // print("shaking!"); if (shakeparam.dir != Vector3.zero) { float randNum = Random.Range(0.2f, 1f); //We use the same random variable in order to randomise only the position of the shake in the dir vector given. float x = randNum * shakeparam.shakeStrength * shakeparam.dir.x; float y = randNum * shakeparam.shakeStrength * shakeparam.dir.y; transform.localPosition = new Vector3(x, y, originalPos.z); } else { //Default shake, randomly centred around player float x = Random.Range(-1f, 1f) * shakeparam.shakeStrength; float y = Random.Range(-1f, 1f) * shakeparam.shakeStrength; transform.localPosition = new Vector3(x, y, originalPos.z); } timeElapsed += Time.deltaTime; } transform.localPosition = Vector3.zero; }
void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.layer == Layer.Walls) { if (stateManager.CurrentState == State.UnstoppableAttack) { stateManager.ReturnToIdle(State.UnstoppableAttack); stateManager.TrySetState(State.Pushed); ContactPoint2D[] contacts = new ContactPoint2D[3]; attackCollider.GetContacts(contacts); moveBody.PushInDirection((Vector2)this.transform.position - contacts[0].point - moveBody.Direction, moveBody.GetMass() * 3); TakeDamage(0); if (TekituCameraShaker.Instance != null) { TekituShakeParameters shakeSettings = new TekituShakeParameters(1, new Vector3(0, 1, 0), 0.5f, 0.4f); StartCoroutine(TekituCameraShaker.Instance.Shake(shakeSettings)); } } } }