another version of this code used to work fine but after swapping out “transform.position =” with .MovePosition it keeps moving my main character to the right without touching anything. Can anyone spot the error? I can get rid of the clamps if it makes it easier
Oof, that is some gnarly code. Not only is it hard to read and debug, it also follows bad performance practices. Here are some tips:
Move many of the calculations to variables.
Use self-documenting names for the variables. This will even help you (the author) understand your code better.
Don’t call GetComponent multiple times for the same component in the same function
Don’t call GetComponent at all in an Update or for user input. Use GetComponent in Start or Awake and store the return value for future usage.
Put your code on multiple lines. This allows you to add breakpoints, or if you’re not familiar with a debugger, you can at least log output to the console easier.
Following that advice, here’s how I’d change the code:
private Rigidbody2D _rigidBody;
private UltimateJoystick _joystick;
private void Start() {
_rigidBody = gameObject.GetComponent<Rigidbody2D>();
_joystick = gameObject.GetComponent<UltimateJoystick>();
}
private void FixedUpdate() {
var axes = new Vector2(
_joystick.GetHorizontalAxis(),
_joystick.GetVerticalAxis()
);
var minX = CameraFollows.leftLimit + objectWidth;
var maxX = CameraFollows.rightLimit - objectWidth;
var goalX = transform.position.x + axes.x * Time.deltaTime * 3f;
var finalX = Mathf.Clamp(goalX, minX, maxX);
var minY = CameraFollows.bottomLimit;
var maxY = CameraFollows.topLimit - objectHeight;
var goalY = transform.position.y + axes.y * Time.deltaTime * 1.5f;
var finalY = Mathf.Clamp(goalY, minY, maxY);
var finalPos = new Vector2(finalX, finalY);
_rigidBody.MovePosition(finalPos);
}
When compiled, it results in nearly the same output as your code (assuming the compiler is doing it’s job). But, it’s much more readable.
–
Now that that’s done, you can start debugging individual lines of code. The best way is to add breakpoints to the lines you want to inspect. But if you’re not familiar with an integrated debugger, you can at least add logging to see what exactly is going on.
For example, here’s a way to get useful information from your code:
Put that right above the _rigidBody.MovePosition line. Play the game in the Editor. Then look at the console output. See if the values make sense
–
Aside from coding and debugging advice, here are a few thoughts:
Is your code running in Update instead of FixedUpdate? You’ll want to use FixedUpdate for physics code.
You’re using Time.deltaTime. This tells me something is wrong. Either your code in Update, or you’re using the wrong deltaTime. In FixedUpdate, you should be using Time.fixedDeltaTime
Lastly, MovePosition follows the Physics2D interpolation settings. Instead of MovePosition, you could try _rigidBody.position = finalPos to “teleport” the object. Not recommended ultimately, but if this works better, it’ll give you some insight into what’s going wrong.
OK so I found out the problem isn’t anywhere in my code but because I checked the “Simulated” box on my rigidbody2d component. Now it doesn’t move at all but the animations work.
That doesn’t really make sense TBH, the problem has to be in your code.
The simulated option is on by default. Unchecking it means there’s nothing in the simulation so it’s pointless having the component there. I don’t see how that relates to “auto moving right”.
Calling MovePosition on a Rigidbody2D that’s not in the simulation will do nothing at all.
so you had code that was not working, you changed something else but the code is still not working, and that makes you conclude that the code is correct?
As already noted in my first reply above, start debugging.
You will know when you are finished debugging either when:
it works
or you can report the issue in these terms:
This is the bare minimum of information to report:
what you want
what you tried
what you expected to happen
what actually happened, log output, variable values, and especially any errors you see
links to actual relevant Unity3D documentation you used to cross-check your work (CRITICAL!!!) (eg, “docs say this, I’m doing this, here’s the code, and it’s not working”)
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?