#This is really vague.
1 messages · Page 1 of 1 (latest)
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:
- It accelerates even when walking, but I only want that to happen when you are running at max speed
- It does not accelerate consecutively when I switch sides. Meaning this acceleration only happens once or twice only when I am still running
You're really going to have to show your code.
yea your right, here: https://gdl.space/owucomadet.cs
the related function are on line 233 with the name handlePlayerSideSwitch()
Ok so what is it you want here exactly?
You want the Lerp on line 241 to happen over multiple frames?
I will say your code is a bit spaghetti
No, I am using the lerp function here to go at speeds between the max speed and the max speed + a number. I want the player to go faster than the max speed and then it will eventually make its way to the max speed. This will all depend on how the graph of the animation curve will look like.
that's not my question though
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
I see, where do I start tho?
and is there a way I can fix all of the issues that I have mentioned?
I'll be honest the code is so complicated as is I would probably rewrite it from scratch
the whole thing?
from scratch
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?
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
ahh I see, yea that makes more sense
is there a reason why we use PascalCases in c#
That is the convention
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.
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
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
ok, but what if I want to add more movement options, will this approach be more appropriate?
When you say that it should be done in one place, do you mean having a function with a bunch of if statements that checks the input and executes the action?