#This is really vague.

1 messages · Page 1 of 1 (latest)

runic cape
#

Yes I see what you mean here, I didn't want to lay out the details and get straight to the problem to make it easier for you to understand.

I saw the solutions that you have laid out and it makes a lot of sense. However, even after trying to implement the solution, it does not really do what I want to do. To put it simply to what I am trying to do, I want the player to accelerate when switching sides (left to right or right to left) in a 2d plain, and the way I did this is by checking the previous input and the current input to detect if that happens, but this only happens in a couple of frames. I have tried to apply the solutions that you suggested me, but there are several problems that came with it:

  1. It accelerates even when walking, but I only want that to happen when you are running at max speed
  2. It does not accelerate consecutively when I switch sides. Meaning this acceleration only happens once or twice only when I am still running
compact zodiac
runic cape
#

the related function are on line 233 with the name handlePlayerSideSwitch()

compact zodiac
#

I will say your code is a bit spaghetti

runic cape
compact zodiac
runic cape
compact zodiac
# runic cape yea ik 😭

A big part of the problem is you seem to refuse to use any local variables or return any values from your functions

#

everything is just a member variable

#

that makes things very confusing because the scope of all the variables is so large, it's hard to know where a variable is going to be read and written from.

#

And you have a lot of functions that are just one line long

#

that should probably just live inline

runic cape
#

and is there a way I can fix all of the issues that I have mentioned?

compact zodiac
#

I'll be honest the code is so complicated as is I would probably rewrite it from scratch

runic cape
#

the whole thing?

compact zodiac
#

from scratch

runic cape
#

ok, what suggestions do you think I should have to make the code better. I know that I should get rid of one line functions, and have more functions return a value. Am I missing anything else?

compact zodiac
#

Also Fix your code style. C# uses PascalCase for methods, not camelCase.

Better method names.

    private void SpeedUpWhenRunning(float rate = 1) {
        accelerationProgress = Mathf.MoveTowards(accelerationProgress, 1, Time.deltaTime * rate);
    }```
SpeedUpWhenRunning is not a good name for this method. This method is more like...  `UpdateAccelrationProgress`
But also that's a one line function that's used in only one place, and should really just... not exist. It's added complication for no benefit.
#

And then stuff like:

    private void setRunningStatus(){
        isRunning = true;
        isSliding = false;
    }

    private void setSlidingStatus(){
        isRunning = false;
        isSliding = true;
    }```
This shows you shouldn't be using two bools for this
#

Maybe an enum would be appropriate.

enum State {
  Walking,
  Running,
  Sliding
}``` for example
runic cape
runic cape
compact zodiac
#

Also why are there 3 speed variables?

    float speedDuringSliding;
    float speedDuringRunning;
    float speedDuringSideSwitching;```
#

that seems unecessary

#

You could just have one

#

Like you have three different places where you set the velocity with 5 diffrerent variables:

rb.velocityX = speedDuringRunning * direction;
rb.velocityX = speedDuringSliding * direction; 
rb.velocityX = speedDuringSideSwitching * direction;    
rb.velocity = new Vector2(direction * crouchWalkingSpeed, rb.velocity.y);
rb.velocity = new Vector2(direction * walkingSpeed, rb.velocity.y);```
#

Those are in 5 different spots

#

you can just have one line that does:

rb.velocityX = currentSpeed * direction;```
#

Other code can set currentSpeed based on whatever is going on.

#

but this turns 5 lines of code into 1, and 5 variables into 1.

runic cape
#

hmmm, I see that makes sense. I did think of that, but I thought it would generate an error.

#

By the way, do you think I made the right choice to use multiple functions in order to detect an input and execute an action just like the way I did?

#

or could I have simply made one function that checks multiple inputs all in one if condition and executed an action there?

#

@compact zodiac

compact zodiac
#

No I think handling all the input should be done more or less in one place

#

this isn't that complicated of an input paradigm that it needs such complexity

runic cape
runic cape
runic cape
#

oh and also, should I have used FixedUpdate instead of Update?

#

if yes then are some of the lines of code that goes in fixed update?

#

(I am sorry if these are too many questions )