HatScript.cs (1755B)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class HatScript : MonoBehaviour { 6 7 // Update is called once per frame 8 public GameObject Owner; 9 private Rigidbody rb; 10 private bool moved = false; 11 12 private void Start() 13 { 14 rb = GetComponent<Rigidbody>(); 15 transform.position = Owner.transform.position; 16 } 17 18 void Update() 19 { 20 Vector3 slimeHead = Owner.transform.position; 21 slimeHead.y = (Owner.transform.position.y + Owner.GetComponent<Collider>().bounds.size.y) + transform.lossyScale.y; 22 23 Vector3 offset = rb.transform.position - slimeHead; 24 if (Vector3.Distance(transform.position, slimeHead) > 0.2f) 25 { 26 Vector3 resultForce = Vector3.zero; 27 resultForce += 20 * offset.normalized; 28 rb.AddForce(-resultForce); 29 moved = true; 30 } 31 32 if (Vector3.Distance(transform.position, slimeHead) < 0.2f && moved) 33 { 34 rb.velocity = Vector3.zero; 35 moved = false; 36 } 37 38 /*Vector3 hatRotation = transform.eulerAngles; 39 hatRotation.y = Mathf.Clamp(hatRotation.y, 0, 20); 40 hatRotation.z = Mathf.Clamp(hatRotation.z, 0, 20); 41 transform.eulerAngles = hatRotation;*/ 42 Vector3 clampPos = slimeHead + Vector3.ClampMagnitude(offset, 0.3f); 43 clampPos.y = Mathf.Clamp(transform.position.y, slimeHead.y + transform.lossyScale.y + 0.25f, slimeHead.y + transform.lossyScale.y + 1); 44 transform.position = clampPos; 45 46 if (rb.angularVelocity.magnitude < 3) 47 { 48 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Owner.transform.forward), 0.5f); 49 } 50 } 51 }