Hi guys, first let me introduce myself
my name’s Hubert, I’m a game programmer with some experience (mostly C++), though I’m new to Unity.
I’ve been reading in documentation and on forums that the proper way of doing physics in Unity is to put all the physics code inside FixedUpdate() and everything else in Update().
Sound’s ok in theory, but in the case of PlayerController which has a camera attached to it (through spring), doing it like this:
/*
Perhaps you recognize that it's slightly modified code taken from one of the official tutorials, so I'd expect it to work properly :)
IIRC initally everything was in FixedUpdate
*/
var walkSpeed = 6.0;
var runMultiplier = 1.25;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate()
{
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
function Update()
{
if (grounded)
{
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
// following line makes movement player orientation dependent
//moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= walkSpeed;
if (Input.GetButton ("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
}
@script RequireComponent(CharacterController)
… introduces subtle jarring movement of the player.
You can see it by yourself in this simple prototype:
http://student.agh.edu.pl/~koshmaar/shared/echo/prototype001.html
Jarring is visible with a fixed timestep = 0.02 (default value), but after increasing it to 0.01 the movement is smooth. I don’t want to change “Fixed Timestep” because it is a global value, so it will have global effect on all physics code - player, enemies, bullets etc possibly a lot of stuff, so by “fixing” the problem in this way, I would be possibly slowing down the physics by 50%.
I’m new to Unity and perhaps it’s acceptable, or the prefered way of doing, but personally it seems to me more like a hack than a fix.
So I was looking for another solution. After moving the code from FixedUpdete() to Update(), the problem has been solved - player movement is very smooth.
However, I’m not sure whether it’s a good way of solving the problem - perhaps Unity / PhysX make some (presumably important) assumptions about the update order, which become invalidated at this point, and certain things in certain areas might not work as expected in the future, making hard to find bugs. Also, is it a viable solution for multiplayer games?
Thanks in advance