using NodeCanvas.Framework; using ParadoxNotion.Design; using ParadoxNotion.Services; using UnityEngine; namespace NodeCanvas.Tasks.Actions { [Category("Core/Movement")] public class ProcessGravity : ActionTask{ public BBParameter gravityScale = 1f; public BBParameter gravityAcceleration = 1f; public BBParameter gravityMax = 8f; private Vector3 currentVelocity; private bool freezeVelocity; private float gravityPower; //Use for initialization. This is called only once in the lifetime of the task. //Return null if init was successfull. Return an error string otherwise protected override string OnInit(){ MonoManager.current.onLateUpdate += LateUpdate; return null; } //This is called once each time the task is enabled. //Call EndAction() to mark the action as finished, either in success or failure. //EndAction can be called from anywhere. protected override void OnExecute() { } //Called once per frame while the action is active. protected override void OnUpdate() { if (!freezeVelocity && agent.velocity != Vector3.zero){ currentVelocity = new Vector3(agent.velocity.x, 0f, agent.velocity.y); } } void LateUpdate(){ if (agent.isGrounded) { gravityPower = 0f; currentVelocity = Vector3.zero; freezeVelocity = true; agent.Move(Physics.gravity * .1f * Time.deltaTime); } else { if (currentVelocity == Vector3.zero && agent.velocity.y < 0f) { freezeVelocity = false; } Debug.Log(currentVelocity); agent.Move((Physics.gravity * gravityPower * Time.deltaTime)); } } //Called when the task is disabled. protected override void OnStop() { EndAction(true); } //Called when the task is paused. protected override void OnPause() { } } }