private void OnEnable() { state = State.NEUTRAL; speed = speedNeutral; angleIndex = Random.Range(0, 4) * 2 + 1; angle = Angles.GetAngle(angleIndex); SetPSysColorLifetime(ps, colorsNeutral); coll.enabled = true; }
private static int DeflectAngleIndex(int angleIndex, Vector2 hitNormal, Vector2 velocity) { float dot = Vector2.Dot(hitNormal, velocity); // 1 = same direction // -1 = opposite direction // 0 = perpendicular // Bounce back if (dot < -0.995f) { angleIndex += 4; } // Bounce to either side if ((dot < +0.7853f && dot > +float.Epsilon) || (dot > -0.7853f && dot < -float.Epsilon)) { Vector3 cross = Vector3.Cross(hitNormal, velocity).normalized; // To right if (cross == Vector3.back) { angleIndex += 2; } // To left if (cross == Vector3.forward) { angleIndex -= 2; } } // Follow the normal if (dot >= -float.Epsilon && dot <= +float.Epsilon) { // convert normal to angle index angleIndex = Angles.GetAngleIndex(Angles.GetRadian(hitNormal)); } // Follow the velocity if (dot >= +0.7853f && dot <= 1f) { // do nothing } angleIndex %= 8; if (angleIndex < 0) { angleIndex = 8 + angleIndex; } return(angleIndex); }
private void FixedUpdate() { Velocity = Vector2.zero; if (inputMove >= 0) { walkDir = inputMove; walkAngle = Angles.GetAngle(inputMove); inputMove = -1; Velocity = Angles.GetDirection(walkAngle) * moveSpeed; swordAnchor.rotation = Quaternion.Euler(0f, 0f, Mathf.Rad2Deg * walkAngle); } rb.MovePosition(rb.position + (Velocity + (hitNormal * bounceForce)) * Time.fixedDeltaTime); hitNormal *= forceFalloff.Evaluate(Mathf.Clamp01((Time.time - hitTime) / 1f)); }
public void Hit(int hitAngleIndex) { if (IsExploding) { return; } onHit.Invoke(gameObject); if (IsCharged) { StartCoroutine(Explosion(4)); } int angleDiff = 8 - angleIndex; int hitAngleRelative = (hitAngleIndex + angleDiff) % 8; // if (hitAngleRelative == 0) { // Debug.Log("from behind"); // } if (hitAngleRelative == 4) { angleIndex += 4; } if (hitAngleRelative > 0 && hitAngleRelative < 4) { angleIndex += 2; } if (hitAngleRelative > 4 && hitAngleRelative < 8) { angleIndex -= 2; } state = State.CHARGED; SetPSysColorLifetime(ps, colorsCharged); speed = speedCharged; angleIndex %= 8; if (angleIndex < 0) { angleIndex = 8 + angleIndex; } angle = Angles.GetAngle(angleIndex); }
private IEnumerator Move(int angleIndex) { Vector2 length = Angles.GetDirection(angleIndex) * moveLength; Vector2 startPos = transform.position; float startTime = Time.time; while (Time.time - startTime < moveDuration) { float dt = (Time.time - startTime) / moveDuration; float pos = positionOverTime.Evaluate(dt); transform.position = startPos + (length * pos); yield return(null); } transform.position = startPos + length; onFreeCoord.Invoke(startPos); onOccupyCoord.Invoke(transform.position); }
private void FixedUpdate() { rb.MovePosition(rb.position + GetVelocity() * Time.fixedDeltaTime); hitNormal = Vector2.zero; for (int i = 0; i < raycastDirs.Length; ++i) { hitNormal += Raycast(raycastDirs[i]); } if (hitNormal == Vector2.zero) { return; } angleIndex = DeflectAngleIndex(angleIndex, hitNormal, GetVelocity().normalized); angle = Angles.GetAngle(angleIndex); rb.MovePosition(rb.position + GetVelocity() * Time.fixedDeltaTime * 2f); }
private Vector2 GetVelocity() { return(Angles.GetDirection(angle) * speed); }