Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Default State Before Idle State

Discussion in 'Animation' started by TheAPGames, Aug 11, 2022.

  1. TheAPGames

    TheAPGames

    Joined:
    Jun 1, 2020
    Posts:
    44
    Hello,

    Thank you for taking the time to read my issue below.

    I have a basic character movement set up. I intend to add a feature where when the level starts, or when the character respawns, a different animation ( Kneeling to stand) plays before transitioning to the idle state. During the Kneeling animation state, id like it to play completely before the player is able to move the character.

    Below is my code for movement. Im achieving the respawn with a game manager script which i added at the bottom as well. Thanks a lot for having a read.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public static PlayerController instance;
    8.  
    9.  
    10.  
    11.     public float rotationSpeed;
    12.     public float jumpHeight;
    13.     public float gravityMultiplier;
    14.     public float jumpButtonGracePeriod;
    15.     public Transform cameraTransform;
    16.     public Animator animator;
    17.     public CharacterController characterController;
    18.     public float jumpHorizontalSpeed;
    19.  
    20.     public Vector3 movementDirection;
    21.  
    22.  
    23.     private float ySpeed;
    24.     private float originalStepOffset;
    25.     private float? lastGroundedTime;
    26.     private float? jumpButtonPressedTime;
    27.     private bool isJumping;
    28.     private bool isGrounded;
    29.  
    30.    
    31.  
    32.  
    33.  
    34.     private void Awake()
    35.     {
    36.         instance = this;
    37.     }
    38.     // Start is called before the first frame update
    39.     void Start()
    40.     {
    41.         originalStepOffset = characterController.stepOffset;
    42.     }
    43.  
    44.     // Update is called once per frame
    45.     void Update()
    46.     {
    47.      
    48.         //Moving
    49.  
    50.         float horizontalInput = Input.GetAxis("Horizontal");
    51.         float verticalInput = Input.GetAxis("Vertical");
    52.  
    53.         movementDirection = new Vector3(horizontalInput, 0, verticalInput);
    54.         float inputMagnitude = Mathf.Clamp01(movementDirection.magnitude);
    55.  
    56.  
    57.         animator.SetFloat("Input Magnitude", inputMagnitude, 0.05f, Time.deltaTime);
    58.  
    59.         movementDirection = Quaternion.AngleAxis(cameraTransform.rotation.eulerAngles.y, Vector3.up) * movementDirection;
    60.         movementDirection.Normalize();
    61.  
    62.  
    63.  
    64.    
    65.  
    66.         //JUMP
    67.         float gravity = Physics.gravity.y * gravityMultiplier;
    68.  
    69.         if(isJumping && ySpeed > 0 && Input.GetButton("Jump") == false)
    70.         {
    71.             gravity *= 2;
    72.         }
    73.  
    74.  
    75.         ySpeed += gravity * Time.deltaTime ;
    76.  
    77.         if (characterController.isGrounded)
    78.         {
    79.             lastGroundedTime = Time.time;
    80.         }
    81.  
    82.         if (Input.GetButtonDown("Jump"))
    83.         {
    84.             jumpButtonPressedTime = Time.time;
    85.         }
    86.  
    87.         if (Time.time - lastGroundedTime <= jumpButtonGracePeriod)
    88.         {
    89.             characterController.stepOffset = originalStepOffset;
    90.             ySpeed = -0.5f;
    91.             animator.SetBool("IsGrounded", true);
    92.             isGrounded = true;
    93.             animator.SetBool("IsJumping", false);
    94.             isJumping = false;
    95.             animator.SetBool("IsFalling", false);
    96.  
    97.             if (Time.time - jumpButtonPressedTime <= jumpButtonGracePeriod)
    98.             {
    99.                 ySpeed = Mathf.Sqrt(jumpHeight * -3 * gravity);
    100.                 animator.SetBool("IsJumping", true);
    101.                 isJumping = true;
    102.                 jumpButtonPressedTime = null;
    103.                 lastGroundedTime = null;
    104.             }
    105.         }
    106.         else
    107.         {
    108.             characterController.stepOffset = 0;
    109.             animator.SetBool("IsGrounded", false);
    110.             isGrounded = false;
    111.  
    112.             if ((isJumping && ySpeed < 0) || ySpeed < -2)
    113.             {
    114.                 animator.SetBool("IsFalling", true);
    115.             }
    116.         }
    117.  
    118.         if (movementDirection != Vector3.zero)
    119.         {
    120.             animator.SetBool("IsMoving", true);
    121.  
    122.             Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
    123.  
    124.             transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
    125.         }
    126.         else
    127.         {
    128.             animator.SetBool("IsMoving", false);
    129.         }
    130.  
    131.         if (isGrounded == false)
    132.         {
    133.             Vector3 velocity = movementDirection * inputMagnitude * jumpHorizontalSpeed;
    134.             velocity.y = ySpeed;
    135.  
    136.             characterController.Move(velocity * Time.deltaTime);
    137.         }
    138.  
    139.      
    140.  
    141.     }
    142.  
    143.  
    144.  
    145.  
    146.     private void OnAnimatorMove()
    147.     {
    148.         if (isGrounded)
    149.         {
    150.             Vector3 velocity = animator.deltaPosition;
    151.             velocity.y = ySpeed * Time.deltaTime;
    152.  
    153.             characterController.Move(velocity);
    154.         }
    155.     }
    156. }
    157.  
    158.  
    159.  
    160.  
    161.  
    162.  
    163.  
    164.  

    Game Manager:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.  
    8.     public static GameManager instance;
    9.  
    10.     private Vector3 respawnPosition;
    11.     private Vector3 camPosition;
    12.  
    13.     public float reSpawnTime;
    14.  
    15.     private void Awake()
    16.     {
    17.         instance = this;
    18.     }
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         Cursor.visible = false;
    24.         Cursor.lockState = CursorLockMode.Locked;
    25.  
    26.         respawnPosition = PlayerController.instance.transform.position ;
    27.         camPosition = CameraController.instance.transform.position ;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.        
    34.     }
    35.  
    36.  
    37.     public void Respawn()
    38.     {
    39.  
    40.         StartCoroutine(RespawnCo());
    41.  
    42.  
    43.     }
    44.  
    45.     private IEnumerator RespawnCo()
    46.     {
    47.         PlayerController.instance.gameObject.SetActive(false);
    48.         CameraController.instance.theCMBrain.enabled = (false);
    49.  
    50.         yield return new WaitForSeconds(reSpawnTime);
    51.  
    52.  
    53.         PlayerController.instance.transform.position = respawnPosition;
    54.         CameraController.instance.theCMBrain.enabled = (true);
    55.         PlayerController.instance.gameObject.SetActive(true);
    56.  
    57.  
    58.     }
    59.  
    60. }
    61.  
     

    Attached Files:

    • 1.png
      1.png
      File size:
      106.8 KB
      Views:
      153