136 lines
4.3 KiB
C#
136 lines
4.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Invector.vCharacterController.AI;
|
|
using Invector;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class vControlAIJump : MonoBehaviour
|
|
{
|
|
private static readonly int JumpHit = Animator.StringToHash("JumpHit");
|
|
|
|
[SerializeField]
|
|
private Vector3 positionOffset;
|
|
[SerializeField]
|
|
[Range(10f, 80f)]
|
|
private float angle = 45f;
|
|
[SerializeField]
|
|
private float jumpDelay = 0.5f;
|
|
[SerializeField]
|
|
private float minDistanceToStartLanding = 2f;
|
|
private vControlAIMelee vControlAIMelee;
|
|
private Rigidbody rb;
|
|
private Animator animator;
|
|
private bool inAir;
|
|
private Vector3 destination;
|
|
private bool isLanding;
|
|
[Header("Debug")]
|
|
[Tooltip("Invoke Jump")]
|
|
public bool jump;
|
|
|
|
private void Awake()
|
|
{
|
|
vControlAIMelee = GetComponent<vControlAIMelee>();
|
|
rb = GetComponent<Rigidbody>();
|
|
animator = GetComponent<Animator>();
|
|
}
|
|
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (jump)
|
|
{
|
|
jump = false;
|
|
Jump();
|
|
}
|
|
|
|
if (inAir && !isLanding)
|
|
{
|
|
if (Vector3.Distance(transform.position, destination) < minDistanceToStartLanding)
|
|
{
|
|
StartLanding();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void JumpOnPlayerNow()
|
|
{
|
|
if (vControlAIMelee != null)
|
|
{
|
|
vControlAIMelee.SetCurrentTarget(Player.Instance.transform);
|
|
transform.forward = (Player.Instance.transform.position - transform.position);
|
|
//Jump();
|
|
}
|
|
}
|
|
|
|
public void Jump()
|
|
{
|
|
if (vControlAIMelee != null && vControlAIMelee.currentTarget != null && vControlAIMelee.currentTarget.transform != null && rb != null)
|
|
{
|
|
//vControlAIMelee.JumpTo(vControlAIMelee.currentTarget.transform.position + positionOffset);
|
|
StartCoroutine(JumpDelay());
|
|
}
|
|
}
|
|
|
|
private IEnumerator JumpDelay()
|
|
{
|
|
animator.CrossFadeInFixedTime("JumpMove", .1f);
|
|
yield return new WaitForSeconds(jumpDelay);
|
|
inAir = true;
|
|
isLanding = false;
|
|
animator.ResetTrigger(JumpHit);
|
|
//vControlAIMelee.enabled = false;
|
|
vControlAIMelee.isJumping = true;
|
|
destination = vControlAIMelee.currentTarget.transform.position + positionOffset;
|
|
FireAtPoint(destination, rb);
|
|
yield return null;
|
|
}
|
|
|
|
private void StartLanding()
|
|
{
|
|
if (!isLanding)
|
|
{
|
|
isLanding = true;
|
|
animator.SetTrigger(JumpHit);
|
|
}
|
|
}
|
|
|
|
private void OnCollisionEnter(Collision collision)
|
|
{
|
|
//Debug.Log("Collision: " + collision.collider.name);
|
|
if (inAir)
|
|
{
|
|
inAir = false;
|
|
//vControlAIMelee.enabled = true;
|
|
vControlAIMelee.isJumping = false;
|
|
StartLanding();
|
|
}
|
|
}
|
|
|
|
private void FireAtPoint(Vector3 point, Rigidbody rigidbody)
|
|
{
|
|
var velocity = BallisticVelocity(point, angle);
|
|
//Debug.Log("Firing at " + point + " velocity " + velocity.magnitude);
|
|
|
|
rigidbody.AddForce(velocity, ForceMode.VelocityChange);
|
|
}
|
|
|
|
private Vector3 BallisticVelocity(Vector3 destination, float angle)
|
|
{
|
|
Vector3 dir = destination - transform.position; // get Target Direction
|
|
float height = dir.y; // get height difference
|
|
dir.y = 0; // retain only the horizontal difference
|
|
float dist = dir.magnitude; // get horizontal direction
|
|
float a = angle * Mathf.Deg2Rad; // Convert angle to radians
|
|
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle.
|
|
dist += height / Mathf.Tan(a); // Correction for small height differences
|
|
|
|
// Calculate the velocity magnitude
|
|
float velocity = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
|
|
return velocity * dir.normalized; // Return a normalized vector.
|
|
}
|
|
}
|
|
}
|