Search Unity

[Still not solved] Casted spell slows down after passing over dead enemies body?

Discussion in 'Scripting' started by Sargaxon, Aug 24, 2016.

  1. Sargaxon

    Sargaxon

    Joined:
    Aug 20, 2016
    Posts:
    23
    My spells work just fine (spells are prefabs/projectiles, shooting them with a script), they are casted to my mouse pointer location and deal damage after colliding with an enemy, the enemy plays a death animation (in that time collision with that enemy is disabled), it starts sinking through the ground and I destroy the enemy gameobject after 2 seconds. Meanwhile, if a spell is cast over the dead body, it will pass normally to the desired direction, but massively slow down or slowly start going to the right/left.

    Any ideas what might be causing this?

    Here are the main functions that deal with this issue, if I've mistakenly programmed something wrong.

    Enemy death function:
    Code (CSharp):
    1.     void Death ()
    2.     {
    3.         isDead = true;
    4.  
    5.         capsuleCollider.isTrigger = true; //when enemy dies, they aren't an obstacle anymore
    6.  
    7.         anim.SetTrigger(deadTrigger); //perform death animation
    8.  
    9.         enemyAudio.clip = deathClip;
    10.         enemyAudio.Play (); //play deathclip
    11.  
    12.         //meshCollider.convex = false;
    13.     }
    14.  
    After the enemy dies, at the end of the death animation an event is called that starts the sinking:
    Code (CSharp):
    1.  public void StartSinking ()
    2.     {
    3.         GetComponent <NavMeshAgent> ().enabled = false;
    4.         GetComponent <Rigidbody> ().isKinematic = true; //ignore rigidbody
    5.         isSinking = true;
    6.         ScoreManager.score += scoreValue;
    7.         LevelManager.playerExperience += experiencePoints;
    8.         Destroy (gameObject, 2f); //destroy gameobject after 2sec
    9.     }
    My spells (projectiles) collision script:
    Code (CSharp):
    1. void OnCollisionEnter(Collision collision)
    2.     {
    3.         if (collision.gameObject.layer == 9) //shootable layer
    4.         {
    5.             ContactPoint contact = collision.contacts[0];
    6.             Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
    7.             Vector3 pos = contact.point;
    8.             Instantiate(explosionPrefab, pos, rot);
    9.             EnemyHealth enemyHealth = collision.gameObject.GetComponent<EnemyHealth>(); //if we hit something, get its script
    10.             if (enemyHealth != null)
    11.             {
    12.                 enemyHealth.TakeDamage(PlayerCasting.spellDamage, contact.point);
    13.                 //Destroy(collider.gameObject);
    14.             }
    15.  
    16.             //destroy the projectile that just caused the trigger collision
    17.             Destroy(gameObject);
    18.         }
    19.     }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    whats the spell/projectile movement code?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    any reason you turn the collider into a trigger rather than just disabling the collider entirely?
     
  4. Sargaxon

    Sargaxon

    Joined:
    Aug 20, 2016
    Posts:
    23
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         spellTimer += Time.deltaTime;
    4.  
    5.         if (Input.GetButton("Fire2") && spellTimer >= spellCooldown && Time.timeScale != 0)
    6.         {
    7.             Cast();
    8.         }
    9.     }
    10.  
    11.     void Cast()
    12.     {
    13.  
    14.         spellTimer = 0f; //reset attackTimer; wait till next shot
    15.  
    16.         gunAudio.Play();
    17.  
    18.         GameObject spell = (GameObject)Instantiate(spellPrefab, spellSpawn.position, spellSpawn.rotation);
    19.         spell.GetComponent<Rigidbody>().velocity = spell.transform.forward * 10;
    20.  
    21.         Destroy(spell, 10.0f);    
    22.     }
    I tried disabling it entirely, didn't help. I enable the trigger for an another functionality, used for OnTriggerEnter, doesn't have anything to do with OnCollisionEnter, as far as I know.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    hmmm... without an enabled collider I don't think it can be the physics engine causing the slow down then, it shouldn't have detected any collision to trigger anything changing. Although having said that without detecting a collision of some sort it shouldn't be triggering any scripts that might be affecting it either...

    how complex at the enemies? do they have colliders on child gameobjects etc.? you're only disabling/altering the collider on the enemy root...

    does the slow down happen:

    only during the death animation (might be an animation thing somehow?),
    only during the slide into the ground after the animation (erm... :confused: ),
    during both?


    complete longshot; have you checked it's not just an overall fps drop, i.e. the whole game slows down rather than the projectile slowing and the rest is fine?
     
  6. Sargaxon

    Sargaxon

    Joined:
    Aug 20, 2016
    Posts:
    23
    Enemies are very simple, only the main gameobject has the usual needed stuff (animator, rigidbody, navmesh agent, audio, capsule and sphere collider and a few scripts for movement, health, attack etc)

    CastSpeed is set to 1sec, so the soonest I can cast the spell is when the death animation is already over and the enemy lies on the floor and starts sinking. After 2 seconds (when the dead enemies gameobject gets destroyed) the spells can be cast over the death location normally. So it should be something with the enemy, although I'm not quite sure what it could be.

    It isn't a fps drop for sure, as the second cast goes faster than the spell that went over the dead body, even passes the previous casted spell. In addition, the rest of the game works normally.