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.

55 lines
1.7 KiB

  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 void Start()
  18. {
  19. transform.forward = Vector3.up;
  20. startDir = transform.forward;
  21. }
  22. // Update is called once per frame
  23. void FixedUpdate()
  24. {
  25. float ratio = (Mathf.Sin(Time.time * Speed) + 1) / 2;
  26. transform.forward = Quaternion.AngleAxis(Mathf.Lerp(AngleLimit.x, AngleLimit.y, ratio), transform.parent.InverseTransformDirection(RotationAxis)) * startDir;
  27. Vector3 hitDirection = Quaternion.AngleAxis(Mathf.Lerp(AngleLimit.x, AngleLimit.y, ratio), transform.parent.InverseTransformDirection(RotationAxis)) * transform.parent.InverseTransformDirection(HitDirection) * -(Mathf.Cos(Time.time * Speed));
  28. Debug.DrawRay(transform.position, hitDirection * Force, Color.green);
  29. }
  30. private void OnTriggerStay(Collider other)
  31. {
  32. var horse = other.GetComponent<PlayerController>();
  33. if (horse != null)
  34. {
  35. float ratio = (Mathf.Sin(Time.time * Speed) + 1) / 2;
  36. Vector3 hitDirection = Quaternion.AngleAxis(Mathf.Lerp(AngleLimit.x, AngleLimit.y, ratio), transform.parent.InverseTransformDirection(RotationAxis)) * transform.parent.InverseTransformDirection(HitDirection) * -(Mathf.Cos(Time.time * Speed));
  37. horse.AddForce((hitDirection.normalized + Vector3.up) * Force);
  38. Debug.Log("Hitting horse");
  39. }
  40. }
  41. }