Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted Bones of my 2d rig get destroyed at runtime

Discussion in 'Animation' started by MastaPlatypus, Mar 16, 2021.

  1. MastaPlatypus

    MastaPlatypus

    Joined:
    Apr 16, 2019
    Posts:
    4
    https://imgur.com/2Dn2kJr (here's a gif of the problem)
    pretty much what the title says, i am experimenting with unity's 2d animation and inputsystem to build a simple fighting game, there are no scripts with GameObject.Destroy() anywhere so i really dont understand what's going on during runtime.
    this is the script that i use to control the character if it helps:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using static UnityEngine.InputSystem.InputAction;
    6.  
    7. public class Character : MonoBehaviour
    8. {
    9.     public float speed;
    10.     public float jump;
    11.     public GameObject sprite;
    12.     public LayerMask mask;
    13.     public float currentdamage = 0;
    14.     [SerializeField]
    15.     private List<BoxCollider> colliders;
    16.     private Animator animator;
    17.     private Rigidbody rb;
    18.     private Vector2 movement_input;
    19.     private BoxCollider col;
    20.     private string facing = "left";
    21.     private bool isjumping = false;
    22.     private bool isdoublejumping = false;
    23.     private bool isgrounded;
    24.     private bool iscrouching = false;
    25.     private bool isfalling = false;
    26.     private Renderer[] spriteRenderers;
    27.     private Controls controls;
    28.     private PlayerConfiguration playerconfiguration;
    29.    
    30.     // Start is called before the first frame update
    31.     void Start(){
    32.         animator = sprite.GetComponent<Animator>();
    33.         rb = GetComponent<Rigidbody>();
    34.         col = GetComponent<BoxCollider>();
    35.         spriteRenderers = sprite.GetComponentsInChildren<Renderer>();
    36.         controls = new Controls();
    37.     }
    38.  
    39.     void FixedUpdate(){
    40.         rb.MovePosition(transform.position + (new Vector3(movement_input.x, 0, 0) * speed * Time.deltaTime));
    41.     }
    42.  
    43.     void Update(){
    44.         isgrounded = Physics.BoxCast(col.bounds.center, new Vector3(col.bounds.extents.x, 0, col.bounds.extents.z), Vector3.down, Quaternion.identity, col.bounds.extents.y + 0.2f, mask);
    45.         if(isgrounded){
    46.             if(isjumping == true || isdoublejumping == true){
    47.                 isjumping = false;
    48.                 isdoublejumping = false;
    49.                 animator.SetTrigger("land");
    50.             }
    51.         }
    52.     }
    53.  
    54.     public void InitializePlayer(PlayerConfiguration pc){
    55.         playerconfiguration = pc;
    56.         playerconfiguration.input.onActionTriggered += onAction;
    57.     }
    58.  
    59.     private void onAction(CallbackContext obj){
    60.         if(obj.action.name == controls.Match.Movement.name){
    61.             OnMove(obj);
    62.         }
    63.         if(obj.action.name == controls.Match.Jump.name){
    64.             OnJump(obj);
    65.         }
    66.         if(obj.action.name == controls.Match.Attack.name){
    67.             OnAttack(obj);
    68.         }
    69.     }
    70.     public void OnMove(InputAction.CallbackContext context)
    71.     {
    72.         movement_input = context.ReadValue<Vector2>();
    73.         if (movement_input.x > 0){
    74.             FlipSprite("right");
    75.         }
    76.         if (movement_input.x < 0){
    77.             FlipSprite("left");
    78.         }
    79.         if(movement_input.y < 0){
    80.             animator.SetTrigger("crouch");
    81.             iscrouching = true;
    82.         }
    83.         if(movement_input.y >= 0){
    84.             animator.SetTrigger("stand");
    85.             iscrouching = false;
    86.         }
    87.     }
    88.  
    89.     public void OnJump(InputAction.CallbackContext context)
    90.     {
    91.         if (context.started){
    92.             if (isgrounded == true){
    93.                 Jump();
    94.                 isjumping = true;
    95.             }
    96.             if (isgrounded == false && isjumping == true){
    97.                 Jump();
    98.                 isjumping = false;
    99.                 isdoublejumping = true;
    100.             }
    101.         }
    102.         void Jump(){
    103.             rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    104.             rb.AddForce(Vector3.up * jump);
    105.             animator.SetTrigger("jump");
    106.         }
    107.     }
    108.  
    109.     public void OnAttack(InputAction.CallbackContext context)
    110.     {
    111.         if (context.started){
    112.             if(!iscrouching){
    113.                 animator.SetTrigger("standing_punch");
    114.             }
    115.             else{
    116.                 animator.SetTrigger("crouching_punch");
    117.             }
    118.         }
    119.     }
    120.  
    121.     public void FlipSprite(string dir)
    122.     {
    123.         if (dir == "right" && facing == "left"){
    124.             sprite.transform.Rotate(0, 180, 0);
    125.             facing = "right";
    126.         }
    127.         if (dir == "left" && facing == "right"){
    128.             sprite.transform.Rotate(0, -180, 0);
    129.             facing = "left";
    130.         }
    131.     }
    132.     public void RecieveDamage(Collision collision, float damage){
    133.         currentdamage = currentdamage + damage;
    134.         Vector3 direction = (col.bounds.center - collision.GetContact(0).point).normalized;
    135.         rb.AddForce(direction * currentdamage * 0.5f);
    136.     }
    137. }
    138.  
     
unityunity