You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.7 KiB

4 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class HammerController : MonoBehaviour
  5. {
  6. [SerializeField]
  7. private Vector2 AngleLimit;
  8. [SerializeField]
  9. private Vector3 RotationAxis;
  10. [SerializeField]
  11. private Vector3 HitDirection;
  12. [SerializeField]
  13. private float Force;
  14. [SerializeField]
  15. private float Speed;
  16. private Vector3 startDir;
  17. private Vector3 lastHitDir;
  18. private void Start()
  19. {
  20. transform.forward = Vector3.up;
  21. startDir = transform.forward;
  22. }
  23. // Update is called once per frame
  24. void FixedUpdate()
  25. {
  26. float ratio = (Mathf.Sin(Time.time * Speed) + 1) / 2;
  27. float ratioOffset = (Mathf.Cos(Time.time * Speed) + 1) / 2;
  28. transform.forward = Quaternion.AngleAxis(Mathf.Lerp(AngleLimit.x, AngleLimit.y, ratio), transform.parent.TransformDirection(RotationAxis)) * startDir;
  29. lastHitDir = Quaternion.AngleAxis(Mathf.Lerp(AngleLimit.x, AngleLimit.y, ratio), transform.parent.TransformDirection(RotationAxis)) * transform.parent.TransformDirection(HitDirection) * -Mathf.Sign(Mathf.Lerp(AngleLimit.x, AngleLimit.y, ratioOffset));
  30. Debug.DrawRay(transform.position, lastHitDir * Force, Color.green);
  31. }
  32. private void OnTriggerStay(Collider other)
  33. {
  34. var ball = other.GetComponent<BallController>();
  35. if (ball != null)
  36. ball.EatHorse = false;
  37. var horse = other.GetComponent<PlayerController>();
  38. if (horse != null)
  39. {
  40. horse.AddForce((lastHitDir.normalized + Vector3.up) * Force);
  41. Debug.Log("Hitting horse");
  42. return;
  43. }
  44. }
  45. }