slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

RigidbodyMover.cs (3298B)


      1 using System;
      2 using UnityEngine;
      3 
      4 namespace Slimecing.Environment.Moving
      5 {
      6     [RequireComponent(typeof(Rigidbody))]
      7     public class RigidbodyMover : MonoBehaviour
      8     {
      9         [SerializeField] private Rigidbody objectRigidbody;
     10 
     11         [SerializeField] private MovingController moveController;
     12 
     13         public Rigidbody moverRigidbody
     14         {
     15             get => objectRigidbody;
     16             set => objectRigidbody = value;
     17         }
     18 
     19         public Vector3 beginningTickPosition { get; set; }
     20         public Quaternion beginningTickRotation { get; set; }
     21         
     22         public Transform objectTransform { get; private set; }
     23         
     24         public Vector3 beginningSimulationPosition { get; private set; }
     25         public Quaternion beginningSimulationRotation { get; private set; }
     26 
     27         private Vector3 _internalCurrentPosition;
     28 
     29         public Vector3 currentPosition
     30         {
     31             get => _internalCurrentPosition;
     32             private set => _internalCurrentPosition = value;
     33         }
     34 
     35         private Quaternion _internalCurrentRotation;
     36         public Quaternion currentRotation
     37         {
     38             get => _internalCurrentRotation;
     39             private set => _internalCurrentRotation = value;
     40         }
     41 
     42         private void OnEnable()
     43         {
     44             MoverSimulationManager.CheckAlive();
     45             MoverSimulationManager.RegisterMover(this);
     46         }
     47 
     48         private void OnDisable()
     49         {
     50             MoverSimulationManager.UnregisterMover(this);
     51         }
     52 
     53         public void ValidateRigidbody()
     54         {
     55             objectRigidbody.centerOfMass = Vector3.zero;
     56             objectRigidbody.useGravity = false;
     57             objectRigidbody.drag = 0f;
     58             objectRigidbody.angularDrag = 0f;
     59             objectRigidbody.collisionDetectionMode = CollisionDetectionMode.Discrete;
     60             objectRigidbody.isKinematic = true;
     61             objectRigidbody.constraints = RigidbodyConstraints.None;
     62             objectRigidbody.interpolation = MoverSimulationManager.interpolationMethod == SimulationInterpolationMethod.Unity ? RigidbodyInterpolation.Interpolate : RigidbodyInterpolation.None;
     63         }
     64 
     65         private void Awake()
     66         {
     67             objectTransform = transform;
     68             ValidateRigidbody();
     69             
     70             moveController.InitializeMover(this);
     71 
     72             currentPosition = objectRigidbody.position;
     73             currentRotation = objectRigidbody.rotation;
     74             beginningSimulationPosition = objectRigidbody.position;
     75             beginningSimulationRotation = objectRigidbody.rotation;
     76         }
     77 
     78         public void UpdateMoverVelocity(float deltaTime)
     79         {
     80             beginningSimulationPosition = currentPosition;
     81             beginningSimulationRotation = currentRotation;
     82             
     83             moveController.TickMover(out _internalCurrentPosition, out _internalCurrentRotation, deltaTime);
     84 
     85             if (!(deltaTime > 0f)) return;
     86             
     87             objectRigidbody.velocity = (currentPosition - beginningSimulationPosition) / deltaTime;
     88 
     89             Quaternion rotationCurrentToTarget = currentRotation * Quaternion.Inverse(beginningSimulationRotation);
     90             objectRigidbody.angularVelocity = Mathf.Deg2Rad * rotationCurrentToTarget.eulerAngles / deltaTime;
     91         }
     92     }
     93 }