RigidBody2D not moving properly


I’ve seen some Unity tutorials and I’m trying to apply good practices to modularize the code. However, I’m running into some issues while trying to use rigidBody2d.MovePosition().

I have two classes: Player (which defines rules and performs calculations) and PlayerController (uses computed data from Player to interact with Unity itself).


using UnityEngine;  public class Player {     public Vector2 MoveTowards(Vector2 currentPosition, Vector2 direction)     {         return currentPosition + direction * 2f;     } }  


using UnityEngine;  [RequireComponent(typeof(Rigidbody2D))] [RequireComponent(typeof(Animator))] public class PlayerController : MonoBehaviour {      public Player player;      Rigidbody2D rb2d;     Vector2 movementDirection = Vector2.zero;      void Awake()     {         player = new Player();         rb2d = GetComponent<Rigidbody2D>();     }      void Update()     {         movementDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));     }      void FixedUpdate()     {         rb2d.MovePosition(player.MoveTowards(rb2d.position, movementDirection) * Time.fixedDeltaTime);     } }  

What happens visually is the player’s sprite moves (just a tiny bit) towards the given direction and goes back to (x: 0, y: 0) as soon as I release the movement key.

Player on Inspector:
Player on Inspector

What I tested

Inside FixedUpdate, it does not work when I use:

rb2d.MovePosition(player.MoveTowards(rb2d.position, movementDirection) * Time.fixedDeltaTime); 

Inside FixedUpdate, it does not work when I use:

Vector2 controllerNewPosition = rb2d.position + movementDirection * 2f; rb2d.MovePosition(controllerNewPosition * Time.fixedDeltaTime); 

Inside FixedUpdate, it works when I use:

rb2d.MovePosition(rb2d.position + movementDirection * 2f * Time.fixedDeltaTime); 


Maybe that is being caused by the operator + and not creating a new Vector2, instead mutating rb2d.position? While I’m not new to programming, I have no experience with C#, so I don’t know the specifics of the syntax yet.

How could I use player.MoveTowards in this case?


Feeling dumb. Right after publishing the question I realized the glaring mathematical priority issue. I’ve been debugging this for hours. I guess it happens to everyone.

How does moving silently with Greater Invisibility after taking an action work?

This question could be simplified with "How does moving silently away from enemy on same turn after casting Invisibility work", but I’ll present the full scenario in case it makes a difference:

A PC, who does not have special bonus actions, is under Greater Invisibility spell (or other equal effect) which allows actions without breaking invisiblity. At the start of their turn in combat, they are 10 ft (one empty 5 feet square between them on the grid) away from an enemy. They then move 5ft, uses their entire attack action to do a melee attack on the enemy (who does not go down). Then they tell the DM "I want to move back 5 feet, then use rest of my movement silently move away, so the enemy doesn’t know where I actually go."

What should DM respond, by the rules?

The PC can’t use hide action, or any action for that matter, because they already used their action to attack. Movement rules don’t have anything special about silent movement as far as I could find. But it also seems quite unreasonable and suspension-of-disbelief breaking to say "sorry, but you are utterly unable to move silently at this moment".

The reason the PC wants their location to not be known is to prevent the enemy from approaching to melee range and hitting them, in this case. The reason could also be the enemy using a spell like Moonbeam, or PC wanting the enemy to waste a blind ranged attack at PC’s (who could be in full cover now, even) last known location.

It’s of course easy to make a custom ruling here, involving some combination of PC Stealth, enemy Perception, and reduced movement rate, but I’m interested in what the rules say, including any language which supports handling this as part of a custom/improvised action, even though the PC doesn’t have an action to spare.

What is the slowest a character can move while still moving?

Stacking various slow effects, such as difficult terrain, prone, etc. Assuming a 30 ft movement, and the character will dash if possible, but there movement must be greater than zero in the end result. Also, for this experiment, it should not be done on a grid, and movements less that 5 feet or fractions of a number of feet are still considered movement.

How can I find the direction a sprite is moving?

I’m trying to get the direction a sprite is moving (up, down, left, right). My current approach is to get the x and y coordinates of the target and the sprite (the sprite is the thing that I’m trying to get the direction of), and then have booleans be true/false based on the the values of the x/y coordinates (if they are greater than, less than, etc.). I’m not trying to rotate the sprite or change anything about how it’s drawn, just find the direction that it’s moving in. This is what I’m currently trying (this is more of a general idea of what I’m doing, and is pretty rough):

if (Target.Position.X < Position.X && Target.Position.Y < Position.Y - 500) {     IsFacingUp = false;     IsFacingDown = false;     IsFacingLeft = true;     IsFacingRight = false; } else if (Target.Position.X < Position.X && Target.Position.Y > Position.Y + 500) {     IsFacingUp = false;     IsFacingDown = false;     IsFacingLeft = true;     IsFacingRight = false; } else if (Target.Position.X > Position.X && Target.Position.Y < Position.Y - 500) {     IsFacingUp = false;     IsFacingDown = false;     IsFacingLeft = false;     IsFacingRight = true; } else if (Target.Position.X > Position.X && Target.Position.Y > Position.Y + 500) {     IsFacingUp = false;     IsFacingDown = false;     IsFacingLeft = false;     IsFacingRight = true; } else if (Target.Position.Y <= Position.Y - 500) {     IsFacingUp = true;     IsFacingDown = false;     IsFacingLeft = false;     IsFacingRight = false; } else if (Target.Position.Y >= Position.Y + 500) {     IsFacingUp = false;     IsFacingDown = true;     IsFacingLeft = false;     IsFacingRight = false; } 

Moving average over time on data entering stochastically

The examples for using MovingAverage mostly refer to data evenly spaced in time,such as stock values. In typical physics data, events arrive random in time (e.g. radio active decay events, but also if acting in day trading on the stock market). I do not see how I can use the apparatus of MovingAverage and associated evaluations in this case. Do I not understand the function, or should I go ahead and invent my own functions?

Moving between different D&D universes

I have a group of mid-high level players. As part of some D&D shenanigans, I wanted to have them plop into Athas (Dark Sun) as a surprise joke. But knowing them, sadistic as they are, they might want to return.

How would it be possible for them to move between universes or return to their home universe? I know I can just DM it any way I want, but I wanted to ask you all if that’s possible on their behalf. For instance, a would using a wish spell, a gate, or the Plane Shift work (using game mechanics)?

Would Athas be considered a plane?