예제 #1
0
    void OnTriggerEnter(Collider collider) {
      Rigidbody otherRigidbody = collider.attachedRigidbody;
      if (otherRigidbody == null || collider.Equals(justHit) || !collider.CompareTag("TennisBall")) {
        return;
      }

      justHit = collider;
      justHitCooldown = .5f;
      bool forehandBackhand = (new Plane(transform.up, transform.position)).GetSide(transform.position + velocityVector);
      Vector3 raquetNormal = forehandBackhand ? transform.up : transform.up * -1;

      Vector3 reflectedVec = Vector3.zero;
      if (velocityVector.magnitude < 1f) {
        reflectedVec = Vector3.Reflect(otherRigidbody.velocity, raquetNormal);
      } else {
        Vector3 vel = otherRigidbody.velocity.normalized;
        Vector3 reflectedBallVel = new Vector3(vel.x, -vel.y, -vel.z);
        reflectedVec = (reflectedBallVel + raquetNormal * RaquetAnglePriority) / (RaquetAnglePriority + 1);
      }

      otherRigidbody.velocity = Vector3.ClampMagnitude((reflectedVec * (1 + velocityVector.magnitude)) * Bounciness, MaxHitSpeed);
      RaquetAudioSource.volume = Mathf.Min(Mathf.Pow(otherRigidbody.velocity.magnitude / 5f, 5f), 1f);
      RaquetAudioSource.Play();

      TrailRenderer trail = collider.GetComponent<TrailRenderer>();
      trail.enabled = true;
    }