Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need some help!

Discussion in 'Scripting' started by unity_srMIybJwy0a9Rw, Jul 28, 2020.

  1. unity_srMIybJwy0a9Rw

    unity_srMIybJwy0a9Rw

    Joined:
    Jul 27, 2020
    Posts:
    13
    Hey guys! Currently working on a game I need to make and am currently running into two issues:

    1. This one is probably more simple. My player has two attack animations that he should alternate between if the button is pressed. However if the button is not pressed within a short time it will reset to the first attack. I know I probably need a stopwatch but i'm not sure how to do it. I really just need some instructions on how to do that.

    2. My character can jump and land on my tilemap but only if there are two solid grid places taken up. But my tilemap also has like wooden platforms that do not take up a full grid box. Anyone might know whats wrong?

    Im gonna put this post on other forums just in case.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    For question 1, make your own timer, just a float variable:

    Code (csharp):
    1. float timeSinceAttack;
    Then in update:

    Code (csharp):
    1. timeSinceAttack += Time.deltaTime;
    Now when you do the first attack,

    Code (csharp):
    1. timeSinceAttack = 0.0f;  //reset attack timer
    And when you try the attack again you say:

    Code (csharp):
    1. if (timeSinceAttack < 0.5f)
    2. {
    3.   // do second attack
    4. }
    5. else
    6. {
    7.   // go back to first attack
    8. }
    I'm not sure about question 2... could you perhaps draw a diagram or screenshot what you mean, perhaps with the tilegrid visible?
     
  3. unity_srMIybJwy0a9Rw

    unity_srMIybJwy0a9Rw

    Joined:
    Jul 27, 2020
    Posts:
    13
    IMG_7782.jpeg IMG_7785.jpeg
     
  4. unity_srMIybJwy0a9Rw

    unity_srMIybJwy0a9Rw

    Joined:
    Jul 27, 2020
    Posts:
    13
    The first image is from my tile palette and I can jump up and and down just fine, but on the wooden platform, when I jump it does not count as landing and keeps playing my jumping animation and code
     
  5. unity_srMIybJwy0a9Rw

    unity_srMIybJwy0a9Rw

    Joined:
    Jul 27, 2020
    Posts:
    13
    void Update()
    {
    timeSinceAttack += Time.deltaTime;

    if (Input.GetKeyDown(KeyCode.O))
    {
    timeSinceAttack = 0f;
    Attack();

    if (timeSinceAttack < 0.5f )
    {
    Attack2();
    }
    else
    {
    Attack();
    }
    }
    got this and I think its right but it doesnt work. If it helps, in the animator, Attack1 transitions to Attack2 if it becomes true(It is a bool) and back if it becomes false. I feel like this may be the issue now.
     
    Last edited: Jul 28, 2020
  6. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Figure out what in your code is considered landing... apparently the wooden platforms don't have whatever the other platforms have that cause it, perhaps a property in the collider, its layer, its name, could be anything you chose.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Read slowly through your logic. You set time to zero and then ALWAYS do an attack (the first one), then always do an attack2 (the second one) because time will now be zero.

    try this flow (make it syntactically correct obviously):

    count up the timer
    if attack button pressed:
    if time < 0.5
    do attack2
    else
    do attack
    time = 0


    and please use code tags!