#Movement direction

1 messages · Page 1 of 1 (latest)

steel bough
#

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

livid blade
#

it's in the if statement

#

when you see if(gravitySwapped)

#

line 56

steel bough
#

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

livid blade
#

? i don't understand what you mean.

#

sry

steel bough
#

It's alright, I'm not that good in explaining

#

Let me break it down a bit

livid blade
#

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

livid blade
steel bough
#

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

livid blade
#

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

steel bough
#

Alright, you want to change the gravity in just one direction, don't you?

#

So you have bottom and right directions in your case

livid blade
#

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

steel bough
#

Yes, I'm taking about the walls

#

So by right direction I mean when you stick to the right wall

livid blade
#

ye

steel bough
#

That's why I'm asking you whether you intend on sticking on left and top walls as well

livid blade
#

nono just right

steel bough
#

Well, that's strange

livid blade
#

wait, i'll explain it better

steel bough
#

You'd better implement the full logic in case you will be changing it in the future

livid blade
#

ye probably but my plan was to start simple

steel bough
#

Your code has redundant things, you can see that the same code is repeated twice

livid blade
#

and do it only on one wall

#

the arrows on the right wall is what i'd like to be able to do

steel bough
steel bough
steel bough
#

Because you have redundant code, which won't work both left and bottom cases

livid blade
#

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 😄

steel bough
#
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);
steel bough
#

I'll go check it out

#

But this should work, as I have imagined in my mind

livid blade
#

i see thanks 🙂 never use lambda before

#

i used ternary operators tho

steel bough
#

Yes, I meant ternary operator. This is not a lambda, you're right

livid blade
#

private void ReverseVector(Vector2 value) =>
new(value.y, value.x); this is lambda no?

steel bough
#

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;
livid blade
#

that's what i thought

#

but it's still going up and down only

steel bough
#

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?

livid blade
#

ye

#

i see what you mean

steel bough
#

which is done using Input.GetAxisRaw("Horizontal"), which you do too

livid blade
#

yep

#

i do it when gravity is normal

steel bough
#

You should be able to move the character with your Up and Down keys right now

livid blade
#

yessir

#

but it moves up and down

#

not right and left

#

actually

#

left and right moves up and down

#

right now

steel bough
#

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

livid blade
#

yep

steel bough
livid blade
#

exactly

livid blade
steel bough
#

Yes, that's logical

#

You've written your code this way

livid blade
#

ye

steel bough
#
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)

livid blade
#

why is it?

#

i added an if statement when player is sticking to the wall no?

steel bough
#

What happens when you press Up and Down keys on your keyboard, instead of Left and Right?

livid blade
#

nothing

steel bough
#
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 ?

livid blade
#

same as the code written before the if statement

steel bough
#

Yes, so what's the value?

#

Please, print it and see which force you apply

livid blade
#

kk

#

first one is movementdirection - current velocity

#

second one is rb.velocity

steel bough
#

That's the movement on the right wall, yes?

livid blade
#

yer

steel bough
#

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

livid blade
#

it should you said it right

steel bough
#

does it though?

livid blade
#

nop

steel bough
#

alright, could you, please, give me the whole code, including the print you put, once more?

livid blade
#

sure

#

i was testing it out with the z axys

steel bough
livid blade
#

nono

#

but it looks like nothing changes

#

if i put horizontal or vertical in any of the three cordinates

#

wait

steel bough
#

No, you should move the charater on the y axis when it's reversed.

livid blade
#

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

steel bough
#

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

livid blade
#

are you sure? it just looks like if i press left it goes up and if i press right it goes down

steel bough
livid blade
#

but when i press right the sphere slows down

livid blade
#

ye

steel bough
#

So you say it works or what?

livid blade
#

i think there's enough force to move the rigidbody, it's just moving in the wrong direction

steel bough
#

I think we're misunderstanding what right and left means in your context

steel bough
livid blade
#

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

steel bough
#

How does it go "up" and move on the "x coordinates"

#

x is right

livid blade
#

i'll record everything

steel bough
#

alright

livid blade
steel bough
#

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;
livid blade
#

ye

steel bough
#

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

steel bough
#

Or change the Physics.gravity via script, in this case you can move using Rigidbody

#

(which is -9.81 by default)

livid blade
#

i see, ty 🙂 i'll try transform translate

#

ty for your time bro

#

means a lot

steel bough
#

Well, I don't think I helped a lot

livid blade
#

still, you spent a lot of time

#

for a guy you don't even know

steel bough
#

Anyway, tell me the results with changing transform

livid blade
#

sure, i'll do it later

#

gotta go now

#

see ya

steel bough
#

Alright, good luck