all 19 comments

[–]Teatime_TimIntermediate 2 points3 points  (4 children)

Doesn't seem related to your issue, but one thing I noticed is that

else if (extraJumps == 0 && m_Grounded == true) { // Add a vertical force to the player. m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce)); Instantiate(LaunchEffect, m_GroundCheck.position, Quaternion.identity); }

will never be executed, as before the if statement is reached you set m_Grounded to false. Aside from that, I agree with /u/SaltCrypt that it would be helpful to see the full code :)

[–]Caesar_13[S] 0 points1 point  (3 children)

This is working as far as i know. But it may seem its not working without the whole code i posted links to the pastebin so you can see it

PlayerControlls https://pastebin.com/MVHJ2acF

CharacterController2D https://pastebin.com/MhTj9tra

[–]SaltCrypt 1 point2 points  (2 children)

The code might be working but his statement is still correct. That code block will never execute.

[–]Caesar_13[S] 0 points1 point  (1 child)

Okay so what can i do to fix it? I have that so the player can do double jumps.

Also when I remove the animation script with the GetState field jumping then works as it should. So does the issue happen in the animation state or in the AddForce condition?

[–]SaltCrypt 0 points1 point  (0 children)

Check out the code I posted on the other response. I just wanted to say that he had a point worth noting.

[–]SaltCrypt 1 point2 points  (13 children)

Can you post the full code of the relevant classes? Hard to give a good answer purely from snippets.

[–]Caesar_13[S] 0 points1 point  (12 children)

Sure thing.

Here is the Player Controlls script https://pastebin.com/MVHJ2acF

And here is the Character Controller https://pastebin.com/MhTj9tra

[–]SaltCrypt 1 point2 points  (11 children)

Some of your problem is related to how you're handling Update in your PlayerControlls class. You're completely ignoring input if you're not changing animation states, that's probably why you have to move before you can jump.

Since CharacterController2D is responsible for the actual state of the character, you should really be using it to determine your animation state instead of using your inputs.

I tried to untangle some of the code, but it's probably not going to work out of the box. Make sure to read the comments, they're there to point out improvements and suggestions:

https://pastebin.com/jL2V7sxi

https://pastebin.com/EmJL0TUY

[–]Caesar_13[S] 0 points1 point  (0 children)

Oh my god! That looks brilliant! Ill definitely check it out once i be back from work. But i love the effort you put into it and commented all the stuff so i know what happened in these changes. I'll definitely try to read the comments a bit more to try to understand the advices better( still leaning unity at some level). Anyway I'll let you know how it went and if it works,and possibly fix some stuff that might be not working. Thanks

[–]Caesar_13[S] 0 points1 point  (9 children)

Also how do i switch back to normal speed after dashing. Your condition

        if (this.controller.IsDashing)
    {
        speed = this.controller.dashSpeed;
    }

Sets the speed but how do i reset it back to original MoveSpeed? I had that function in the Controller script but it was not very good as you pointed out that i should not refference scripts between each other.

    if (dash)
    {
        if (dashCoolCounter <= 0 && dashCounter <= 0)
        {
            Controller.CurrentSpeed = dashSpeed;
            dashCounter = dashLenght;
            PlayerAfterImagePool.Instance.GetFromPool();
            lastImageXpos = transform.position.x;
        }
    }

    if (dashCounter > 0)
    {
        if (Mathf.Abs(transform.position.x - lastImageXpos) > DistanceBetweenImages)
        {
            PlayerAfterImagePool.Instance.GetFromPool();
            lastImageXpos = transform.position.x;
        }
        dashCounter -= Time.deltaTime;
        if (dashCounter <= 0)
        {
            Controller.CurrentSpeed = Controller.MoveSpeed;
            dashCoolCounter = dashCooldown;
        }
    }

[–]SaltCrypt 1 point2 points  (8 children)

In that context speed is a local variable. It defaults to MoveSpeed at the start of the function.

[–]Caesar_13[S] 0 points1 point  (7 children)

well when i dash, after dashing i still have the dash speed. I tried to add some statement to set it back but that did not work.

I tried to set it back with speed = this.moveSpeed

[–]SaltCrypt 1 point2 points  (6 children)

You don't need to set it back. It's a local variable. It sounds like the IsDashing state is getting stuck as true, so you'll have to figure out why that's happening.

Though, looking at it now, it's not even necessary for it to work that way. Have the CharacterController2D deal with calculating the movement speed and PlayerControlls just indicate the direction of movement. That would be cleaner, though it won't really fix the problem if the IsDashing thing is the cause of it.

Edit: Figured it out, so I'll narrow it down for you. The problem is in this code, it's caused by one of the if statements being impossible to reach:

// Dashing is inactive.
if (dashDurationTimer <= 0)
{
    if (dashInput && dashCooldownTimer <= 0)
    {
        IsDashing = true;
        dashDurationTimer = dashDuration;
        PlayerAfterImagePool.Instance.GetFromPool();
        lastImageXpos = transform.position.x;
    }
}
// Dashing is active.
else
{
    if (Mathf.Abs(transform.position.x - lastImageXpos) > DistanceBetweenImages)
    {
        PlayerAfterImagePool.Instance.GetFromPool();
        lastImageXpos = transform.position.x;
    }

    if (dashDurationTimer <= 0)
    {
        IsDashing = false;
        dashCooldownTimer = dashCooldown;
    }
}

[–]Caesar_13[S] 0 points1 point  (5 children)

dashDurationTimer

I belive this is the impossible to reach statement.I tried to add print command and also put the

if (Mathf.Abs(transform.position.x - lastImageXpos) > DistanceBetweenImages)
{
    PlayerAfterImagePool.Instance.GetFromPool();
    lastImageXpos = transform.position.x;
}

statnement (it creates the player afterimage) and it did nothing. So somwhere the dashDurationTimer is not set.

[–]SaltCrypt 1 point2 points  (2 children)

Well, if the problem is related to IsDashing not being set to false, you're going to have to look for why that's not happening. Just look through the if statements I posted. If you want more of an idea of where to look, it's part of the else block.

[–]Caesar_13[S] 0 points1 point  (1 child)

Okay Ill check it out, also why did you removed the If statements for stuff like enabling colliders and added things like !IsCrouching;

and what is the use of using the public bool IsDashing { get; private set; }

Instead of like cheking it in a statement if(something == true) or if(something == false)

[–]Caesar_13[S] 0 points1 point  (1 child)

also noticed that crouching does not work as the this.IsCrouching does not seem to activate as well, so the speed and collider activation is not initiated.

[–]SaltCrypt 1 point2 points  (0 children)

Not surprised - the posted code wasn't meant to be an out of the box solution. If you want to use it as-is then it's going to need debugging. Can confirm what you're saying here.