I’m trying to create a 2D puzzle game where the gravity changes when you rotate. The default and inverted gravity works fine, but the sideways (90 and 270 degrees) is not working. It doens affect the player, but the player is falling slowly and can move in the air. The player will continuously move, without stopping, when I press one of the movement keys.
The checkQR code is checking how many times the Q or E button has been pressed. Instead of checking the players currently degree, I’m just checking how many times the player has pressed the buttons.
you could also make the gravity always face the transform.down direction, which is the object’s local downward vector. So as you rotate the object, the gravity is always pointing “down” with respect to the object’s rotation.
Thanks for your reply and sorry for my late response. I’d appreciate your help, but it did not work. The player keeps falling slowly while being able to move in the air, and the left and right movement is constant, which means the player wont stop moving 1 direction until I change it
It probably has something to do with the fact that you’re directly setting “velocity” everywhere, which will override the force of gravity, which could be in any direction.
If I were you I would only use AddForce(force, ForceMode2D.Impulse) to move your character, that way all the forces in the physics simulation will be combined and the physics engine will determine the final velocity for you.
So if your character rotates at all, use his local axes for movement. The “force” will always be “direction * speed”, so the force required move your character along his local positive X axis would be “playerTransform.right * moveSpeed”. And be sure to handle physics updates in FixedUpdate.
Is your character rotated? If you want to add a force along your character’s X axis taking into account rotation, you should use “AddForce(transform.right * moveSpeed, ForceMode2D.Impulse)”.
If you want to always move him towards the right side of the screen, regardless of rotation, you can use “AddForce(Vector2.right * moveSpeed, ForceMode2D.Impulse)”.
Thanks for your reply. I got confused from your previous reply, but you have made it clearly now. The movement is working on all the axes, but the player does ignore the gravity/ground at 90 and 270 degrees. The player is able to move in the air, but will be forced back.
I’m not entirely sure what I am looking for in that video. From what I see gravity is indeed acting on the character in all rotations. Do you want the gravity to always pull to the bottom of the screen? If so, you don’t need to change gravity at all for each rotation. How are you doing your jumping / ground check? What do you mean by “forced back”? It may help if you post the full player code as you have it right now so I can look through it without wondering what else could be going on. If you’re not comfortable posting the whole thing here you can send it to me in a private message.
For future reference the problem was that the player+camera were rotating together, and the directional movement forces and gravity needed to use the local right and up vectors of the player+camera, and the all the forces needed to use AddForce to play nicely together.