Make it so that while the attack animation is not over, others could not start using…
- User
Make it so that while the attack animation is not over, others could not start using UnityEditor.Experimental.GraphView; using UnityEngine;
[RequireComponent(typeof(CharacterController))] [RequireComponent(typeof(Animator))] public class ThirdPersonMovement : MonoBehaviour { [SerializeField] private Transform _camera; [SerializeField] private MovementCharacter _characteristics; private float _vertical, _horizontal, _run; private readonly string STR_VERTICAL = "Vertical"; private readonly string STR_HORIZONTAL = "Horizontal"; private readonly string STR_RUN = "Run"; private readonly string STR_JUMP = "Jump"; private readonly string STR_ATTACK = "Attack"; private const float DISTANCE_OFFFSET_CAMERA = 5f; private CharacterController _controller; private Animator _animator; private Vector3 _direction; private Quaternion _look; private Vector3 TargerRotate => _camera.forward * DISTANCE_OFFFSET_CAMERA; private bool Idle => _horizontal == 0.0f && _vertical == 0.0f;
private void Start() { _controller = GetComponent<CharacterController>(); _animator = GetComponent<Animator>(); Cursor.visible = _characteristics.VisibleCursor; } private void Update() { Movement(); Rotate(); } private void Movement() { if(_controller.isGrounded) { _horizontal = Input.GetAxis(STR_HORIZONTAL); _vertical = Input.GetAxis(STR_VERTICAL); _run = Input.GetAxis(STR_RUN); _direction = transform.TransformDirection(_horizontal, 0, _vertical).normalized; PlayAnimation(); Jump(); Attack(); } _direction.y -= _characteristics.Gravity * Time.deltaTime; float speed = _run * _characteristics.RunSpeed + _characteristics.MovementSpeed; Vector3 dir = _direction * speed * Time.deltaTime; dir.y = _direction.y; _controller.Move(dir); } private void Attack() { if(Input.GetButtonDown(STR_ATTACK)) { _animator.SetTrigger(STR_ATTACK); } } private void Jump() { if(Input.GetButtonDown(STR_JUMP)) { _animator.SetTrigger(STR_JUMP); _direction.y += _characteristics.JumpForce; } } private void Rotate() { if(Idle) return; Vector3 target = TargerRotate; target.y = 0; _look = Quaternion.LookRotation(target); float speed = _characteristics.AngularSpeed * Time.deltaTime; transform.rotation = Quaternion.RotateTowards(transform.rotation, _look, speed); } private void PlayAnimation() { float horizontal = _run * _horizontal + _horizontal; float vertical = _run * _vertical + _vertical; _animator.SetFloat(STR_VERTICAL, vertical); _animator.SetFloat(STR_HORIZONTAL, horizontal); }}
- Assistant
To prevent other attacks from starting while the attack animation is not over, you can introduce a boolean variable called "isAttacking".
First, declare the variable at the top of the script:
private bool isAttacking = false;In the
Attack()method, setisAttackingto true and start the attack animation:private void Attack() { if (Input.GetButtonDown(STR_ATTACK) && !isAttacking) { isAttacking = true; _animator.SetTrigger(STR_ATTACK); } }In the
Update()method, check if the attack animation is complete before allowing other attacks to start:private void Update() { if (isAttacking && !_animator.GetCurrentAnimatorStateInfo(0).IsName("Attack")) // Replace "Attack" with the appropriate attack animation state name { isAttacking = false; } Movement(); Rotate(); }Now, other attacks can only start when the
isAttackingvariable is false, which means the attack animation is complete. - User
Make it so that while the attack animation is not over, other animations cannot be played (Run, Jump) using UnityEditor.Experimental.GraphView; using UnityEngine;
[RequireComponent(typeof(CharacterController))] [RequireComponent(typeof(Animator))] public class ThirdPersonMovement : MonoBehaviour { [SerializeField] private Transform _camera; [SerializeField] private MovementCharacter _characteristics; private float _vertical, _horizontal, _run; private readonly string STR_VERTICAL = “Vertical”; private readonly string STR_HORIZONTAL = “Horizontal”; private readonly string STR_RUN = “Run”; private readonly string STR_JUMP = “Jump”; private readonly string STR_ATTACK = “Attack”; private const float DISTANCE_OFFFSET_CAMERA = 5f; private CharacterController _controller; private Animator _animator; private Vector3 _direction; private Quaternion _look; private Vector3 TargerRotate => _camera.forward * DISTANCE_OFFFSET_CAMERA; private bool Idle => _horizontal == 0.0f && _vertical == 0.0f;
- Assistant
To prevent other animations (such as Run and Jump) from playing while the attack animation is not over, you can introduce a boolean variable called "isAttacking".
First, declare the variable at the top of the script:
private bool isAttacking = false;In the
Attack()method, setisAttackingto true and start the attack animation:private void Attack() { if (Input.GetButtonDown(STR_ATTACK) && !isAttacking) { isAttacking = true; _animator.SetTrigger(STR_ATTACK); } }Modify the
Jump()method to check if the character is not attacking before starting the jump animation: - User
Add movement speed modifiers. When jumping and moving backwards, the movement speed should decrease to 75% and 50%, respectively.
- Assistant
To add movement speed modifiers when jumping and moving backwards, you can introduce two variables:
jumpSpeedModifierandbackwardsSpeedModifier. Here's the updated code:[SerializeField] private float jumpSpeedModifier = 0.75f; [SerializeField] private float backwardsSpeedModifier = 0.5f;Modify the
Movement()method to apply the speed modifiers:private void Movement() { // ... float speedModifier = 1f; if (!_controller.isGrounded) { speedModifier *= jumpSpeedModifier; } else if (_vertical < 0f) { speedModifier *= backwardsSpeedModifier; } float speed = speedModifier * (_run * _characteristics.RunSpeed + _characteristics.MovementSpeed); Vector3 dir = _direction * speed * Time.deltaTime; // ... }Now, when jumping, the movement speed will be reduced to 75% of the original speed, and when moving backwards, the movement speed will be reduced to 50% of the original speed.