#Movement direction
1 messages · Page 1 of 1 (latest)
That's where you move the character, right?
Vector3 movementDirection = new Vector3(horizontal, 0f, 1f).normalized * moveSpeed;
rb.AddForce(movementDirection - currentVelocity, ForceMode.Acceleration);
Movement direction
You don't need it though
The movement direction can be assigned in a single line
So you're either on top, buttom, left and right
i made a bool so that when it's true movement should be different
and i made an if statement to made it clear and readable let's say
I appreciate the help anyway 🙂 dw about it
You have 4 different directions, each should have its different vector
- bottom ->
Vector3.right * horizontal - top ->
Vector3.right * -horizontal - right ->
Vector3.up * horizontal - left ->
Vector3.up * -horizontal
So Vector3.right * horizontal is new Vector2(horizontal, 0f, 0f)
oh, I swapped top and bottom, by the way
i see what you mean, but i think it's more clear the way i wrote it no? maybe it's just preference
i might be wrong about it
Alright, you want to change the gravity in just one direction, don't you?
So you have bottom and right directions in your case
i'd like to be able to move left and right when sphere is sticking to the right wall
gravity is already swapped
Vector3 gravity = gravityDirection.normalized * -gravityStrenght;
rb.AddForce(gravity, ForceMode.Acceleration);
here
Yes, I'm taking about the walls
So by right direction I mean when you stick to the right wall
ye
That's why I'm asking you whether you intend on sticking on left and top walls as well
nono just right
Well, that's strange
wait, i'll explain it better
You'd better implement the full logic in case you will be changing it in the future
ye probably but my plan was to start simple
Your code has redundant things, you can see that the same code is repeated twice
and do it only on one wall
the arrows on the right wall is what i'd like to be able to do
Well, you'll have to fully rewrite the logic if you want to go to left and bottom in the future
Yes, I understand what you're doing
why?
Because you have redundant code, which won't work both left and bottom cases
i'd make more 2 if statements for that probably : P
there are 100% better ways to do that
for sure
but it's my first 3d game, and i've been coding for not even 6 months i think
so sorry if my code looks bad
i'm just trying to understand 😄
float horizontal = Input.GetAxisRaw("Horizontal") * (currDirection.x != 0 : 1 : -1);
Vector2 moveDir = currDirection * gravityStrength + ReverseVector(currDirection) * horizontal;
rb.AddForce(currDirection * gravityStrength);
//
private void ReverseVector(Vector2 value) =>
new(value.y, value.x);
Oh, that's an interesting stuff
I haven't tested it, though I took it into a deep consideration
That's how I'd do it
Anyway, I gotta test it
Here currDirection Vector2:
bottom->(0, -1)top->(0, 1)left->(-1, 0)right->(1, 0)
Which is the direction of the force added to your Rigidbody
You do it in these lines
Vector3 gravity = gravityDirection.normalized * -gravityStrenght;
rb.AddForce(gravity, ForceMode.Acceleration);
This should calculate all 4 directions without with a single lambda condition
I'll go check it out
But this should work, as I have imagined in my mind
Yes, I meant ternary operator. This is not a lambda, you're right
private void ReverseVector(Vector2 value) =>
new(value.y, value.x); this is lambda no?
Yes, this is
I was talking about this one currDirection.x != 0 : 1 : -1, which is a ternary operator
Anyway, this code should work, because you apply the force on the Y axis
Vector3 movementDirectionSwapped = new Vector3(0f, vertical , 1f).normalized * moveSpeed;
But I still have questions to getting the Vertical axis, which are Up and Down keys on your keyboard
float vertical = Input.GetAxisRaw("Vertical");
It doesn't matter whether you change the direction, you still want to be able to control the player using your Left and Right keys on the keyboad, don't you?
which is done using Input.GetAxisRaw("Horizontal"), which you do too
You should be able to move the character with your Up and Down keys right now
yessir
but it moves up and down
not right and left
actually
left and right moves up and down
right now
You character is moved up and down on the Y axis when it's sticked to the wall, basically, but it should be seen as if it's moving right and left, as you reverse your camera
yep
what do you mean?
exactly
if i press left the sphere goes up, if i press right it goes down
ye
float horizontal = Input.GetAxisRaw("Horizontal");
Vector3 currentVelocity = rb.velocity;
currentVelocity.y = 0f;
Vector3 movementDirection = new Vector3(horizontal, 0f, 1f).normalized * moveSpeed;
rb.AddForce(movementDirection - currentVelocity, ForceMode.Acceleration);
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity);
This part of the code is executed even when the player is sticked to the wall
(which is redundant)
What happens when you press Up and Down keys on your keyboard, instead of Left and Right?
nothing
Yes, you do both logic for the horizontal and vertical movement when it's sticked
Vector3 movementDirectionSwapped = new Vector3(0f, vertical , 1f).normalized * moveSpeed;
rb.AddForce(movementDirectionSwapped - currentVelocity, ForceMode.Acceleration);
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity);
Here, what's the value of movementDirectionSwapped - currentVelocity and velocity ?
same as the code written before the if statement
kk
first one is movementdirection - current velocity
second one is rb.velocity
That's the movement on the right wall, yes?
yer
Well, it should move since you add the force to the y axis.
it should move the player right and left according to your game coordinates
it should you said it right
does it though?
nop
alright, could you, please, give me the whole code, including the print you put, once more?
Wait, so were the screenshots you sent with the z axis?
nono
but it looks like nothing changes
if i put horizontal or vertical in any of the three cordinates
wait
No, you should move the charater on the y axis when it's reversed.
ye but even by doing that it stays the same
float Horizontal = Input.GetAxisRaw("Horizontal");
Vector3 movementDirectionSwapped = new Vector3(0f, Horizontal , 1f).normalized * moveSpeed;
should be like this
Oh, wait
even if y isn't 0
it's still not enough force for the rigidbody to push the player, as it's not lying on the floor
I completely forgot it
Rigidbody won't be able to move like this
are you sure? it just looks like if i press left it goes up and if i press right it goes down
This code is going to work if you move it using transform.Translate
but when i press right the sphere slows down
While on the right wall?
ye
So you say it works or what?
i think there's enough force to move the rigidbody, it's just moving in the wrong direction
I think we're misunderstanding what right and left means in your context
Alright, when you on the right wall. Is the player moved up and right according to Unity's coordinates?
i'll tell you
by pressing the "a" on the keyboard the sphere goes up and move on the x coordinates. If i press "d" it's the opposite
but i should move on the y axys
so yeah, we're moving on the wrong axys
even tho i've set horizontal on the y coordinate in the code
i'll record everything
alright
Alright, that's what I meant
You have changed this code to move on the x axis, right?
Vector3 movementDirectionSwapped = new Vector3(0f, vertical , 1f).normalized * moveSpeed;
ye
This code is not going to work, as the Rigidbody called move on nothing
You can move on the X axis, because of the floor under it
But you cannot move on the Y axis, because you don't have the floor there
The Rigidbody uses Physics.
It's the same as if you can move right and left in your life as a human, but cannot move up and down, because of the gravity applied to you
Anyway, this code is going to work if you use e.g. transform.Translate
Or change the Physics.gravity via script, in this case you can move using Rigidbody
(which is -9.81 by default)
Well, I don't think I helped a lot
Anyway, tell me the results with changing transform
Alright, good luck