Hello all,
As part of a script I’m writing, I require that the world (which consist of several terrains side by side) be reset once the player moves a certain distance from an origin point (x = 0, z = 0 most likely). Please don’t worry why I’m doing this, it’s not important to the problem at hand
!
Basically a reset consist of moving the world back to some starting position, and also moving the player some amount so they don’t notice the move. Everything works fine for the most part, but the problem is sometimes the player goes through the terrain when a reset occurs, which is obviously no good!
Everything is reset/moved in the same frame, so I really don’t understand how the player can go through the terrain, but that is probably an issue for another place/time.
I am using a character controller and basic FPSWalker script (From the island demo I believe).
@script RequireComponent(CharacterController)
class FPSWalker extends MoveController
{
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var lastVelocity : Vector3;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.smoothDeltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
function FreezeMovement()
{
lastVelocity = moveDirection;
GetComponent(CharacterController).Move(Vector3.zero);
}
function UnfreezeMovement()
{
moveDirection = lastVelocity;
var flags = GetComponent(CharacterController).Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
}
Basically from my other script which has a reference of this FPSWalker script, I am calling FreezeMovement, then moving the terrains and player (via transform.position), then calling UnfreezeMovement. I am unsure if this is even freezing the character’s movement as I desire, but I do know it does not solve the problem of the player going through the terrain.
So basically what I’m looking for is a way to freeze the player mid frame, do something, then have them resume at their pre-freeze velocity in the same frame. Anyone have any ideas?
Unfortunately, I can’t just “use a rigidbody” or something else, as this script is actually being designed as a tool other people will use (the resetting functionality is just one optional part to the script), so there’s no way for me to know how they will control their character. I actually still need to test rigidbodies to see if they present a similar problem as the character controller, but for now I just want to focus on the character controller.
Thanks in advance for any help!