Search Unity

Ran Into a Problem...

Discussion in 'Animation' started by Bolchev, Aug 3, 2015.

  1. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    Hello everyone,

    For a while now as a hobby I've been developing a small space shooter game. At the beginning I was using mostly scripts from quill18creates(Big thank you!), but as I started understanding a little bit of code here and there I started to adapt my game and introduce lines of code here and there and even managed to create a few basic scripts of my own. Anyway the game info+screens is on opengameart game dev forums it's called Final Frontier(probably gonna change) if any of you are interested.
    Recently, however I ran into a problem. I wanted to make my little game be more real with animations. Mostly I wanted to make a simple destruction animation for players and enemy's (and flying by asteroids). I made the animation but whatever I tried to do the animation wouldn't run or would run but not properly.
    I tried using and animator controller at first using the parameters, but I think somehow I couldn't connect the parameter health created in the Animator Controller and the already scripted parameter for health I've given to my player.
    I also tried numerous codes running only the animation, which would have been perfect as I really do not want to go through there(my game is probably gonna include very simple animation systems, just animations when things get destroyed and maybe of the ships jets when the player moves forward).
    Finally spawning an empty gameobject with the animation and a simple script to just play the animation but that didn't work either... So here I am broken after all night of research and failure here is my basic hp script (clean from my failures :p) player hp script I would appreciate any help thank you !
    P.S Sorry for my English it's not my native language.


    using UnityEngine;
    using System.Collections;

    public class Damage9Handler : MonoBehaviour {

    int health = 6;

    float invulnPeriod = 2.7f;
    float invulnTimer = 0;
    int correctLayer;

    SpriteRenderer spriteRend;

    void Start() {
    correctLayer = gameObject.layer;

    // NOTE! This only get the renderer on the parent object.
    // In other words, it doesn't work for children. I.E. "enemy01"
    spriteRend = GetComponent<SpriteRenderer>();

    if(spriteRend == null) {
    spriteRend = transform.GetComponentInChildren<SpriteRenderer>();

    if(spriteRend==null) {
    Debug.LogError("Object '"+gameObject.name+"' has no sprite renderer.");
    }
    }
    }

    void OnTriggerEnter2D() {
    health--;

    if(invulnPeriod > 0) {
    invulnTimer = invulnPeriod;
    gameObject.layer = 10;
    }
    }

    void Update() {

    if(invulnTimer > 0) {
    invulnTimer -= Time.deltaTime;

    if(invulnTimer <= 0) {
    gameObject.layer = correctLayer;
    if(spriteRend != null) {
    spriteRend.enabled = true;
    }
    }
    else {
    if(spriteRend != null) {
    spriteRend.enabled = !spriteRend.enabled;
    }
    }
    }

    if(health <= 0) {
    Die();
    }
    }

    void Die() {
    Destroy(gameObject);
    }

    }
     
  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    I don't see any Animator or Animation Components referenced in your script! You'll need to attach one of those (You should probably use Mecanim) and pass in parameter data into it. However even if your game has "simple" animations, you'll still need an idle state. So you'll need 2 animations: one for the default state, and another for "destruction" as you stated above. Then pass in the variable "dead" or whatever you want (you can pass in health as well) and just check for that in animation--add a transition when dead is true.

    P.S. in void Die() you destroy the GameObject. If you destroy it, obviously you won't see any animations... because it's gone.
     
  3. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    Thank you so much I got it running thanks to your suggestions, as for the destroyed game object I managed to get around that by using WaitForSeconds ( time of animation)... However now a Different problem popped up although I think I have know the cause at least. I use an empty object and a spawning script to spawn enemy prefabs at random locations out of player view. To stop the enemys from shooting or moving in the 1 second the animation plays I went into their movement and shooting scripts and added if statements reffering to my player hp scripts. I got an error that health wasn't static so I went into the health script and made it static.
    Now if the first enemy is destroyed all enemys die and all spawning enemys immediately die as well... here is the health script for the enemy:


    using UnityEngine;
    using System.Collections;

    public class DamageCHandler : MonoBehaviour {

    public static int health = 1;

    float invulnPeriod = 0;
    float invulnTimer = 0;
    int correctLayer;
    public int scoreValue = 1;
    Animator Destruction;
    IEnumerator Timer()
    {
    yield return new WaitForSeconds (1.1f);
    Die ();
    }
    SpriteRenderer spriteRend;

    void Start() {
    Destruction = GetComponent <Animator> ();
    correctLayer = gameObject.layer;

    // NOTE! This only get the renderer on the parent object.
    // In other words, it doesn't work for children. I.E. "enemy01"
    spriteRend = GetComponent<SpriteRenderer>();

    if(spriteRend == null) {
    spriteRend = transform.GetComponentInChildren<SpriteRenderer>();

    if(spriteRend==null) {
    Debug.LogError("Object '"+gameObject.name+"' has no sprite renderer.");
    }
    }
    }

    void OnTriggerEnter2D() {
    health--;

    if(invulnPeriod > 0) {
    invulnTimer = invulnPeriod;
    gameObject.layer = 10;
    }
    }

    void Update() {

    if(invulnTimer > 0) {
    invulnTimer -= Time.deltaTime;

    if(invulnTimer <= 0) {
    gameObject.layer = correctLayer;
    if(spriteRend != null) {
    spriteRend.enabled = true;
    }
    }
    else {
    if(spriteRend != null) {
    spriteRend.enabled = !spriteRend.enabled;
    }
    }
    }

    if(health <= 0) {
    Destruction.SetBool("Death", true);
    StartCoroutine (Timer ());
    }
    }

    void Die() {
    APlScore.score += scoreValue;
    Destroy(gameObject);
    }

    }
     
  4. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    Nevermind solved the problem by creating a new game object when enemy or player dies (the explosion is a separate game prefab)
    Ty again fore helping me with my first problem