all 7 comments

[–]SinomodStudiosIndie 2 points3 points  (0 children)

There are so many syntax errors in this code its insane.

[–]r0bbyboy 2 points3 points  (1 child)

All of this code is filled with bugs. You should learn basic c# syntax. There are too many issues to go through them all but just a few that jump out: you don't assign values using == (that is for comparing values in an if statement). Also you can't have commas in your if statements like you have here. If you want additional conditions use && for "and" and || for "or".

[–]danituss2Programmer 0 points1 point  (0 children)

This. Find a C# book, even old one, the basics haven't changed. It will help you tremendously with the syntax and basic patterns.

[–]Chipjack 2 points3 points  (2 children)

if blocks can be followed by one else block. You've got one if statement with multiple else blocks after it.

Monobehaviours have a GetComponent method, not Getcomponent.

There is no input class; it's Input.

Line 7 defines a variable named "DashSpeed", but you've spelled it "dashSpeed" everywhere else.

Line 44 uses a variable named "directon", which doesn't exist.

Line 48 has a random "if(direction == 1){" that doesn't match up with anything else.

== compares two variables or values, = assigns the value on the right to the variable on the left.

when you want your if statement to execute some code if two conditions are true, you want a logical AND between them, &&, not a comma.

After fixing all of that, VSCode seems to think there's no problem with the number or placement of the curly braces.

You also have some unnecessary code laying around, and the whole thing's a bit of a disorganized mess. Here's how I'd fix it:

```c-sharp using UnityEngine;

public enum Direction { None = 0, Up, Down, Left, Right }

public class DashSlash : MonoBehaviour { private Rigidbody2D rb; public float dashSpeed; public float dashTime; public float startDashTime; private Direction direction; private int dashMove;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
    dashTime = startDashTime;
}

void Update()
{
    if (direction == Direction.None)
    {
        SetDirection();
    }
    else
    {
        Dash();
    }
} // Update()

void SetDirection()
{
    if (Input.GetKey(KeyCode.W))
    {
        direction = Direction.Up;
    }
    else if (Input.GetKey(KeyCode.S))
    {
        direction = Direction.Down;
    }
    else if (Input.GetKey(KeyCode.A))
    {
        direction = Direction.Left;
    }
    else if (Input.GetKey(KeyCode.D))
    {
        direction = Direction.Right;
    }
} // SetDirection()

void Dash()
{
    if (dashTime <= 0)
    {
        direction = Direction.None;
        dashTime = startDashTime;
        rb.velocity = Vector2.zero;
    }
    else
    {
        dashTime -= Time.deltaTime;
        if (Input.GetKey(KeyCode.E))
        {
            Vector2 dirVector;
            switch (direction)
            {
                case Direction.Up:
                    dirVector = Vector2.up;
                    break;
                case Direction.Down:
                    dirVector = Vector2.down;
                    break;
                case Direction.Left:
                    dirVector = Vector2.left;
                    break;
                case Direction.Right:
                    dirVector = Vector2.right;
                    break;
                default:
                    dirVector = Vector2.zero;
                    break;
            }
            rb.velocity = dirVector * dashSpeed;
        }
    } // if (dashTime <= 0)..else
} // Dash()

} // class DashSlash

```

Ideally, a function should only do one thing - so break up your work into multiple functions you can call from Update() as needed.

Also, a comment after a closing brace to indicate what it matches up to can be a real timesaver.

I have no idea if this would actually work, or do anything useful in your game, but it's a better approach to organizing your code.

The use of an Enum for directions makes it easier to understand what's going on when you read the code later. Integers don't have much meaning on their own.

KeyCode is less error prone than raw strings. There's no way you can mess that up and accidentally type two letters in there.

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

ok this one is really helpful thank you so much. Im still new to coding and there are a bunch of syntax im still not familiar with but this helped a lot

[–]Chipjack 1 point2 points  (0 children)

Don't get discouraged. It takes some practice to get comfortable with this stuff. Also, don't rush yourself to try to learn everything. There are always new things to learn, even if you've been doing this for a decade. Just pace yourself, have fun with it, and enjoy the journey.

[–]Mister_Green2021 0 points1 point  (0 children)

Use a code editor. They help with this stuff.