Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Issue with a Class in Finite State Machine (Unity)

Good day/night! I've been working on making a Match 3 game in Unity for a project and I'm 99% done but there is an issue. After the player makes a match, the pieces aren't falling to fill the empty squares and I'm not sure what the reason is. Disclaimer: This is a HW assignment where I had to turn an enum to a finite state machine. It's basically done, but I only have this issue. I'm not asking anyone to do the assignment for me. As I said before, I did it. I just have this error and wanted to get another set of eyes to view the code and point out anything off about it.

Currently after the player makes a match, the pieces aren't falling down for some reason. The class that's supposed to handle it is the MovingState Class and this is what I have at the moment:

C#:
public override void Update(Animal animal)
    { //Animal refers to the Animal Class where the states are put into action
        Debug.Log("Moving...");

        animal.lerpTime += Time.deltaTime;
       if (animal.lerpTime > animal.lerpTimeMax)
       {
            animal.lerpTime = animal.lerpTimeMax;
            //animal.ChangeState(animal.idle);

       }

        if (animal.lerpTimeMax != 0) //Prevents an invalid input error
        {
            //shrinks the game objs
            animal.transform.localScale = Vector3.Lerp(animal.lerpOrigin, animal.lerpTarget, animal.lerpTime / animal.lerpTimeMax);
            
        }

        if (animal.transform.localScale == animal.lerpTarget) 
        {
            change_move = true; //This bool was declared in the beginning of the class so it's not the issue 
            //Debug.Log("LerpT is LS");
        }

        if (change_move)
        {   
            animal.ChangeState(animal.idle);
            change_move = false;
        }
    }

This is her original code.:

C#:
if (State == AnimalState.Moving) //using an enum
        {
            lerpTime += Time.deltaTime;
            if (lerpTime > lerpTimeMax)
            {
                lerpTime = lerpTimeMax;
                State = AnimalState.Idle;
            }

            transform.position = Vector3.Lerp(lerpOrigin, lerpTarget, lerpTime / lerpTimeMax);
        }

I made sure not too make too many changes to her code since it did the job, but I'm not sure what I'm doing wrong. I noticed that in her original code it did change the state back to idle after the conditional. I tried that, but it didn't work either. Any tips or tutorials are appreciated!
 
Wish I knew the answer for you. Have you tried adding in more debug log messages? That's where I would start I'd have debug.log after almost every line of code, displaying any data on any variables being passed, i.e.:
C#:
if (animal.lerpTimeMax != 0) //Prevents an invalid input error
        {
            //shrinks the game objs
            animal.transform.localScale = Vector3.Lerp(animal.lerpOrigin, animal.lerpTarget, animal.lerpTime / animal.lerpTimeMax);
            Debug.Log("animial lerpOrigin="+animal.lerpOrigin+" target="+animal.lerpTarget);
        }
 

Buy us a coffee!

Back
Top Bottom