0

Just starting out with unity, I simply just want to move the object forward in a straight-line. However, there are two things I'm noticing and I'm not sure why they're happening.

First, before starting the program, the Player's (Cube) transform position is set to (0, 1, 0). When I first simply run the program, the position changes to (-3.429751e-08, 1, -2.532359e-08) without me touching or doing anything further.

The second issue I'm having is, when pressing the forward key, "w" in this case, position x will slowly increase while rotation y slowly decreases. So the Player slowly drifts to the right while rotating slowly to the left.

All Slippery does is delete all friction from Ground. PlayerMat is just to change the color of the cube.

Basic code, Full Screen before start, Screen after start before and after moving are attached:

PlayerMovement Code

Full Screen BEFORE Starting Program

Screen AFTER Starting but didn't do anything else. Observe the random change of positions x and z

Screen AFTER Starting and moving forward a bit. Observe the random increase of position x and random decrease of rotation y

I tried resetting the position to (0, 1, 0) at the start using my Movement script as well as manually doing it in transform, but it stays (-3.429751e-08, 1, -2.532359e-08) either way. I also tried updating the rotation every frame to keep the rotation (0, 0, 0) using my Movement Script. However, the Player then drifts to the left.

Predict
  • 1
  • 1
  • Please do not post screenshots of code, post the code in textual form instead. – taiyo Feb 01 '23 at 17:14
  • This is just floating point inaccuracies -> [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – derHugo Feb 01 '23 at 18:41

1 Answers1

0

Number representations in computers always have rounding errors because the space to represent numbers (e.g. 32 bit for float) is discrete. -3.429751e-08 is a very small value and can be considered zero. For the same reasons these little rounding errors stack up with repeated calculations. You are using the physics engine of Unity which will do a lot of calculations in the background pushing your cube around and thus changing the transform. The occuring rotational drift is a "natural" consequence of these calculations. That why you can tell the rigid body component to constraint such unwanted drifts. In your rigid body component, look under Contraints > Freeze Rotation and check the Y box. This should fix your movement.

taiyo
  • 535
  • 1
  • 7
  • Thank you for your answer @taiyo. I did notice that and checked it. This did fix my issue... however it doesn't seem optimal. What if I did want to allow for rotation eventually? I guess I could uncheck that constraint through the code if the user pressed whatever button is designated to allowing for rotation, then recheck it after. Is your solution the only way to stop this from happening? In the tutorial I was watching, they did not have any constraints checks and didn't have any issues with random rotation nor drifting. Could it be dependent upon your system or Unity version? – Predict Feb 01 '23 at 20:02
  • The freezing of Y rotation did fix the rotating. However, it still drifts to the right. Message was too long for me to edit that in. – Predict Feb 01 '23 at 20:07
  • Hmm hard to tell. Wouldn't say its a version thing. Are your objetcs scaled? Physics don't like scaled objects. In general, if you rely on non-kinematic physics, you'll have to live with the results of the engine and add tweaks that correct such unwanted behaviour. A kinematic body gives you much more control, but you have to implement most collision stuff then yourself. – taiyo Feb 01 '23 at 20:22
  • Yup, the ground is scaled. However, it appears to have been my "Slippery" physic material. In the tutorial, all the user did was set the friction to 0 for this physic material, and attached it to the ground. His then worked fine, didn't have the problem I am. Looking more at it, I then tried adding the same "Slippery" physic material to the player and it fixed the issue. I guess there was friction from the player against the frictionless surface and that causes the little increase/decrease. I appreciate your time and help @taiyo, thank you. – Predict Feb 02 '23 at 21:32