poprawki materialy unity 6, pajaki, podmiana katalogu FSM w AIControlerze, zmiana w Spider 2.0 Behaviours , skopane zycie bylo, ladowalo pajaka z zyciem 2.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
namespace Invector.vCharacterController.AI
|
||||
{
|
||||
@@ -9,23 +10,11 @@ namespace Invector.vCharacterController.AI
|
||||
}
|
||||
|
||||
[vClassHeader("AI Controller")]
|
||||
public abstract class vAIMotor : vHealthController, vICharacter, vEventSystems.vIAnimatorStateInfoController
|
||||
public abstract class vAIMotor : vCharacter, vEventSystems.vIAnimatorStateInfoController
|
||||
{
|
||||
#region AI VARIABLES
|
||||
|
||||
#region Inspector Variables
|
||||
|
||||
public enum DeathBy
|
||||
{
|
||||
Animation,
|
||||
AnimationWithRagdoll,
|
||||
Ragdoll
|
||||
}
|
||||
|
||||
[vEditorToolbar("Health")]
|
||||
public DeathBy deathBy = DeathBy.Animation;
|
||||
public bool removeComponentsAfterDie = true;
|
||||
|
||||
#region Inspector Variables
|
||||
[vEditorToolbar("Start")]
|
||||
public bool disableControllerOnStart;
|
||||
|
||||
@@ -38,8 +27,6 @@ namespace Invector.vCharacterController.AI
|
||||
[vHelpBox("When checked, make sure to reset the speed values to 1 to use the root motion original speed, increase or decrease this value to have extraSpeed", vHelpBoxAttribute.MessageType.Info)]
|
||||
[Tooltip("Turn off if you have 'in place' animations and use this values above to move the character, or use with root motion as extra speed")]
|
||||
public bool useRootMotion = false;
|
||||
[Tooltip("Use TurnOnSpot animations")]
|
||||
public bool turnOnSpotAnim = true;
|
||||
public vMovementSpeed freeSpeed, strafeSpeed;
|
||||
|
||||
|
||||
@@ -89,8 +76,6 @@ namespace Invector.vCharacterController.AI
|
||||
[vEditorToolbar("Events")]
|
||||
public UnityEngine.Events.UnityEvent onEnableController;
|
||||
public UnityEngine.Events.UnityEvent onDisableController;
|
||||
[SerializeField] protected OnActiveRagdoll _onActiveRagdoll = new OnActiveRagdoll();
|
||||
public OnActiveRagdoll onActiveRagdoll { get { return _onActiveRagdoll; } protected set { _onActiveRagdoll = value; } }
|
||||
#endregion
|
||||
|
||||
#region Hide Inspector Variables
|
||||
@@ -114,7 +99,7 @@ namespace Invector.vCharacterController.AI
|
||||
|
||||
public vEventSystems.vAnimatorStateInfos animatorStateInfos { get; protected set; }
|
||||
|
||||
public bool isCrouching
|
||||
public override bool isCrouching
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -125,20 +110,19 @@ namespace Invector.vCharacterController.AI
|
||||
_isCrouching = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool isRolling { get; protected set; }
|
||||
public vAIMovementSpeed movementSpeed { get { return currentSpeed; } protected set { currentSpeed = value; } }
|
||||
public bool useCustomRotationSpeed { get; set; }
|
||||
public float customRotationSpeed { get; set; }
|
||||
private UnityEngine.Events.UnityEvent onUpdateAI = new UnityEngine.Events.UnityEvent();
|
||||
public UnityEngine.Events.UnityEvent OnUpdateAI { get { return onUpdateAI; } }
|
||||
private bool _isStrafingRef;
|
||||
private bool _isGroundedRef;
|
||||
private float _verticalVelocityRef;
|
||||
private float _groundDistanceRef;
|
||||
private bool _isCrouchingRef;
|
||||
private bool _isCrouchingFromCast;
|
||||
|
||||
private bool _isCrouching;
|
||||
protected UnityEvent onUpdateAI = new UnityEvent();
|
||||
public UnityEvent OnUpdateAI { get { return onUpdateAI; } }
|
||||
protected bool _isStrafingRef;
|
||||
protected bool _isGroundedRef;
|
||||
protected float _verticalVelocityRef;
|
||||
protected float _groundDistanceRef;
|
||||
protected bool _isCrouchingRef;
|
||||
protected bool _isCrouchingFromCast;
|
||||
|
||||
public virtual bool actions
|
||||
{
|
||||
@@ -158,20 +142,16 @@ namespace Invector.vCharacterController.AI
|
||||
protected float strafeMagnitude;
|
||||
protected float rotationMagnitude;
|
||||
protected float colliderHeight;
|
||||
protected float verticalVelocity;
|
||||
protected float verticalVelocity;
|
||||
protected RaycastHit groundHit;
|
||||
protected Quaternion freeRotation;
|
||||
protected Vector3 colliderCenter;
|
||||
protected Vector3 lastCharacterAngle;
|
||||
protected Vector3 temporaryDirection;
|
||||
protected float temporaryDirectionTime;
|
||||
#endregion
|
||||
|
||||
#region Animator Variables
|
||||
protected vAnimatorParameter hitDirectionHash;
|
||||
protected vAnimatorParameter reactionIDHash;
|
||||
protected vAnimatorParameter triggerReactionHash;
|
||||
protected vAnimatorParameter triggerResetStateHash;
|
||||
protected vAnimatorParameter recoilIDHash;
|
||||
protected vAnimatorParameter triggerRecoilHash;
|
||||
#region Animator Variables
|
||||
public AnimatorStateInfo baseLayerInfo, rightArmInfo, leftArmInfo, fullBodyInfo, upperBodyInfo, underBodyInfo;
|
||||
public virtual int baseLayer { get { return animator.GetLayerIndex("Base Layer"); } }
|
||||
public virtual int underBodyLayer { get { return animator.GetLayerIndex("UnderBody"); } }
|
||||
@@ -179,8 +159,8 @@ namespace Invector.vCharacterController.AI
|
||||
public virtual int leftArmLayer { get { return animator.GetLayerIndex("LeftArm"); } }
|
||||
public virtual int upperBodyLayer { get { return animator.GetLayerIndex("UpperBody"); } }
|
||||
public virtual int fullbodyLayer { get { return animator.GetLayerIndex("FullBody"); } }
|
||||
[HideInInspector]
|
||||
public bool triggerDieBehaviour;
|
||||
//[HideInInspector]
|
||||
//public bool triggerDieBehaviour;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -225,8 +205,7 @@ namespace Invector.vCharacterController.AI
|
||||
protected override void Start()
|
||||
{
|
||||
base.Start();
|
||||
if (!animator)
|
||||
animator = GetComponent<Animator>();
|
||||
animator = GetComponent<Animator>();
|
||||
|
||||
if (animator)
|
||||
{
|
||||
@@ -278,11 +257,13 @@ namespace Invector.vCharacterController.AI
|
||||
{
|
||||
DisableAIController();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
if (animatorStateInfos != null)
|
||||
if (animatorStateInfos != null && animator)
|
||||
{
|
||||
animatorStateInfos.RegisterListener();
|
||||
}
|
||||
@@ -290,7 +271,7 @@ namespace Invector.vCharacterController.AI
|
||||
|
||||
protected virtual void OnDisable()
|
||||
{
|
||||
if (animatorStateInfos != null)
|
||||
if (animatorStateInfos != null && animator)
|
||||
{
|
||||
animatorStateInfos.RemoveListener();
|
||||
}
|
||||
@@ -307,7 +288,6 @@ namespace Invector.vCharacterController.AI
|
||||
|
||||
protected virtual void UpdateAI()
|
||||
{
|
||||
HealthControl();
|
||||
UpdateLocomotion();
|
||||
UpdateAnimator();
|
||||
onUpdateAI.Invoke();
|
||||
@@ -331,7 +311,7 @@ namespace Invector.vCharacterController.AI
|
||||
this.targetDirection = targetDirection.normalized;
|
||||
this.input = Vector3.Lerp(this.input, input, smooth * Time.deltaTime);
|
||||
}
|
||||
|
||||
|
||||
protected virtual void UpdateLocomotion()
|
||||
{
|
||||
StepOffset();
|
||||
@@ -358,8 +338,9 @@ namespace Invector.vCharacterController.AI
|
||||
{
|
||||
FreeMovement();
|
||||
}
|
||||
}
|
||||
if (temporaryDirectionTime > 0) temporaryDirectionTime -= Time.deltaTime;
|
||||
|
||||
}
|
||||
|
||||
protected virtual void StrafeMovement()
|
||||
{
|
||||
@@ -372,13 +353,21 @@ namespace Invector.vCharacterController.AI
|
||||
var rotDir = targetDirection.normalized;
|
||||
|
||||
rotDir.y = 0;
|
||||
if (rotDir.magnitude > 0.1f && input.magnitude > 0.4f && !isRolling && !ragdolled)
|
||||
if (rotDir.magnitude > 0.1f && input.magnitude > 0.1f && !isRolling && !ragdolled)
|
||||
{
|
||||
if (!lockRotation)
|
||||
{
|
||||
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(rotDir, transform.up), rotationSpeed * Time.deltaTime);
|
||||
Rotate(rotDir);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (temporaryDirectionTime > 0)
|
||||
{
|
||||
Rotate(temporaryDirection);
|
||||
}
|
||||
}
|
||||
|
||||
animator.SetFloat("InputMagnitude", isJumping ? 0 : strafeMagnitude, inputMagnitudeSmooth, Time.deltaTime);
|
||||
}
|
||||
|
||||
@@ -434,7 +423,8 @@ namespace Invector.vCharacterController.AI
|
||||
animator.SetFloat("InputMagnitude", isJumping ? 0 : speed, inputMagnitudeSmooth, Time.deltaTime);
|
||||
|
||||
var conditions = (!actions);
|
||||
if (input != Vector3.zero && targetDirection.magnitude > 0.2f && conditions)
|
||||
|
||||
if (input.magnitude > 0.1f && targetDirection.magnitude > 0.2f && conditions)
|
||||
{
|
||||
Vector3 lookDirection = targetDirection.normalized;
|
||||
freeRotation = Quaternion.LookRotation(lookDirection, transform.up);
|
||||
@@ -442,9 +432,30 @@ namespace Invector.vCharacterController.AI
|
||||
var euler = new Vector3(transform.eulerAngles.x, eulerY, transform.eulerAngles.z);
|
||||
if (!lockRotation && !isRolling && speed > 0.1f && !ragdolled)
|
||||
{
|
||||
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(euler), rotationSpeed * Time.deltaTime);
|
||||
Rotate(Quaternion.Euler(euler));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (temporaryDirectionTime > 0)
|
||||
{
|
||||
Rotate(temporaryDirection);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Rotate(Vector3 targetDirection)
|
||||
{
|
||||
|
||||
targetDirection.y = 0f;
|
||||
if (targetDirection.magnitude > 0.1f)
|
||||
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(targetDirection, Vector3.up), rotationSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
protected virtual void Rotate(Quaternion targetRotation)
|
||||
{
|
||||
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
protected virtual bool isSprinting
|
||||
@@ -582,27 +593,26 @@ namespace Invector.vCharacterController.AI
|
||||
_capsuleCollider.height = colliderHeight;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void HealthControl()
|
||||
public new bool isDead
|
||||
{
|
||||
if (isGrounded && isDead)
|
||||
get => base.isDead;
|
||||
set
|
||||
{
|
||||
_rigidbody.isKinematic = true;
|
||||
_capsuleCollider.enabled = false;
|
||||
}
|
||||
|
||||
// If the player has lost all it's health and the death flag hasn't been set yet...
|
||||
if (currentHealth > 0 && isDead)
|
||||
{
|
||||
isDead = false;
|
||||
_rigidbody.isKinematic = false;
|
||||
_capsuleCollider.enabled = true;
|
||||
triggerDieBehaviour = false;
|
||||
|
||||
if (deathBy == DeathBy.Animation || deathBy == DeathBy.AnimationWithRagdoll)
|
||||
base.isDead = value;
|
||||
if (value)
|
||||
{
|
||||
animator.SetBool("isDead", isDead);
|
||||
if (isGrounded)
|
||||
{
|
||||
if (_rigidbody) _rigidbody.isKinematic = true;
|
||||
if (_capsuleCollider) _capsuleCollider.enabled = false;
|
||||
}
|
||||
}
|
||||
else if (!ragdolled)
|
||||
{
|
||||
if (_rigidbody) _rigidbody.isKinematic = false;
|
||||
if (_capsuleCollider) _capsuleCollider.enabled = true;
|
||||
}
|
||||
if (animator) animator.SetBool("isDead", value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -689,16 +699,12 @@ namespace Invector.vCharacterController.AI
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Animator animator { get; protected set; }
|
||||
|
||||
public virtual bool ragdolled { get; set; }
|
||||
|
||||
protected virtual void AnimatorLocomotion()
|
||||
{
|
||||
var canMove = !stopMove && !lockMovement && !animatorStateInfos.HasTag("LockMovement");
|
||||
animator.SetFloat("InputHorizontal", canMove && isStrafing && !isSprinting ? direction : 0f, .2f, Time.deltaTime);
|
||||
animator.SetFloat("InputVertical", canMove ? speed : 0f, .2f, Time.deltaTime);
|
||||
|
||||
|
||||
IsStrafingAnim = isStrafing;
|
||||
IsGroundedAnim = isGrounded;
|
||||
IsCrouchingAnim = isCrouching;
|
||||
@@ -742,21 +748,15 @@ namespace Invector.vCharacterController.AI
|
||||
{
|
||||
if (!isDead) return;
|
||||
|
||||
if (!triggerDieBehaviour)
|
||||
{
|
||||
triggerDieBehaviour = true;
|
||||
TriggerDeath();
|
||||
}
|
||||
|
||||
// death by animation
|
||||
if (deathBy == DeathBy.Animation)
|
||||
{
|
||||
int deadLayer = 0;
|
||||
var info = animatorStateInfos.GetStateInfoUsingTag("Dead");
|
||||
if (info!=null)
|
||||
{
|
||||
if (!animator.IsInTransition(deadLayer) && info.normalizedTime >= 0.99f && GroundDistanceAnim <= 0.15f)
|
||||
{
|
||||
if (info != null)
|
||||
{
|
||||
if (!animator.IsInTransition(deadLayer) && info.normalizedTime >= 0.99f && GroundDistanceAnim <= 0.15f)
|
||||
{
|
||||
RemoveComponents();
|
||||
}
|
||||
}
|
||||
@@ -766,12 +766,11 @@ namespace Invector.vCharacterController.AI
|
||||
{
|
||||
int deadLayer = 0;
|
||||
var info = animatorStateInfos.GetStateInfoUsingTag("Dead");
|
||||
if (info!=null)
|
||||
{
|
||||
if (info != null)
|
||||
{
|
||||
if (!animator.IsInTransition(deadLayer) && info.normalizedTime >= 0.8f && GroundDistanceAnim <= 0.15f)
|
||||
{
|
||||
onActiveRagdoll.Invoke(null);
|
||||
RemoveComponents();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -779,21 +778,8 @@ namespace Invector.vCharacterController.AI
|
||||
else if (deathBy == DeathBy.Ragdoll)
|
||||
{
|
||||
onActiveRagdoll.Invoke(null);
|
||||
RemoveComponents();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void TriggerDeath()
|
||||
{
|
||||
// change the culling mode to render the animation until finish
|
||||
animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
|
||||
// trigger die animation
|
||||
if (deathBy == DeathBy.Animation || deathBy == DeathBy.AnimationWithRagdoll)
|
||||
{
|
||||
animator.SetBool("isDead", isDead);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void RemoveComponents()
|
||||
{
|
||||
if (!removeComponentsAfterDie)
|
||||
@@ -825,7 +811,7 @@ namespace Invector.vCharacterController.AI
|
||||
|
||||
protected virtual void ControlSpeed(float velocity)
|
||||
{
|
||||
if (Time.deltaTime == 0 || isJumping)
|
||||
if (Time.deltaTime == 0 || isJumping || _rigidbody.isKinematic)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -868,14 +854,14 @@ namespace Invector.vCharacterController.AI
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected virtual void ActionsControl()
|
||||
{
|
||||
// to have better control of your actions, you can filter the animations state using bools
|
||||
// this way you can know exactly what animation state the character is playing
|
||||
|
||||
isRolling = baseLayerInfo.IsName("Roll");
|
||||
|
||||
|
||||
// locks player movement while a animation with tag 'LockMovement' is playing
|
||||
UpdateLockMovement();
|
||||
UpdateLockRotation();
|
||||
@@ -907,7 +893,7 @@ namespace Invector.vCharacterController.AI
|
||||
// use root rotation for custom actions or
|
||||
if (customAction || input.magnitude < 0.1f)
|
||||
{
|
||||
_rigidbody.linearVelocity = Vector3.zero;
|
||||
//_rigidbody.velocity = Vector3.zero;
|
||||
_rigidbody.position = animator.rootPosition;
|
||||
transform.rotation = animator.rootRotation;
|
||||
|
||||
@@ -915,7 +901,7 @@ namespace Invector.vCharacterController.AI
|
||||
}
|
||||
if (lockMovement)
|
||||
{
|
||||
_rigidbody.linearVelocity = Vector3.zero;// _rigidbody.velocity.Set(0, _rigidbody.velocity.y, 0);
|
||||
//_rigidbody.velocity = Vector3.zero;
|
||||
_rigidbody.position = animator.rootPosition;
|
||||
if (lockRotation)
|
||||
{
|
||||
@@ -928,7 +914,6 @@ namespace Invector.vCharacterController.AI
|
||||
{
|
||||
_rigidbody.rotation = animator.rootRotation;
|
||||
}
|
||||
_rigidbody.rotation = Quaternion.LookRotation(_rigidbody.rotation * Vector3.forward, Vector3.up);
|
||||
|
||||
var a_strafeSpeed = Mathf.Abs(strafeMagnitude);
|
||||
|
||||
@@ -1176,12 +1161,8 @@ namespace Invector.vCharacterController.AI
|
||||
animator.SetFloat("InputHorizontal", 0f);
|
||||
animator.SetFloat("InputVertical", 0f);
|
||||
animator.SetFloat("InputMagnitude", 0f);
|
||||
if (turnOnSpotAnim)
|
||||
{
|
||||
animator.SetFloat("RotationMagnitude", rotationMagnitude, rotationMagnitude == 0 ? 0.1f : 0.01f, Time.fixedDeltaTime);
|
||||
}
|
||||
}
|
||||
_rigidbody.linearVelocity = Vector3.zero;
|
||||
//_rigidbody.velocity = Vector3.zero;
|
||||
_rigidbody.isKinematic = true;
|
||||
//_capsuleCollider.isTrigger = true;
|
||||
enabled = false;
|
||||
@@ -1255,7 +1236,7 @@ namespace Invector.vCharacterController.AI
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void ResetRagdoll()
|
||||
public override void ResetRagdoll()
|
||||
{
|
||||
lockMovement = false;
|
||||
verticalVelocity = 0f;
|
||||
@@ -1266,7 +1247,7 @@ namespace Invector.vCharacterController.AI
|
||||
_capsuleCollider.enabled = true;
|
||||
}
|
||||
|
||||
public virtual void EnableRagdoll()
|
||||
public override void EnableRagdoll()
|
||||
{
|
||||
animator.SetFloat("InputHorizontal", 0f);
|
||||
animator.SetFloat("InputVertical", 0f);
|
||||
|
||||
Reference in New Issue
Block a user