I’ve attempted some amateur surgery on the PlatformerController script bundled with the 2D Platformer tutorial and have opened up a bit of a problem.
After moving the character around for a little while, he will start to drift on his own. The strength of this drift increases over time to the point where walking against it is not possible. I suspect it has something to do with one of the Time.deltaTime expressions, specifically those within ApplyGravity, but I’m stabbing in the dark.
Perhaps you can spot the leak?
*Taking another look at this old project, I’ve been able to learn a little more about the problem. The amount of drift (left or right) seems tied to the total distance covered by the character in that direction. It’s as if some integer is increasing/decreasing with right/left movement and nudging the character along as a result.
Any ideas would be very much appreciated.
// Does this script currently respond to Input?
var canControl = true;
// The character will spawn at spawnPoint's position when needed. This could be changed via a script at runtime to implement, e.g. waypoints/savepoints.
var spawnPoint : Transform;
class BoyControllerMovement {
// The speed when walking
var walkSpeed = 3.0;
var inAirControlAcceleration = 1.0;
// The gravity for the character
var gravity = 60.0;
var maxFallSpeed = 20.0;
// How fast does the character change speeds? Higher is faster.
var speedSmoothing = 5.0;
// This controls how fast the graphics of the character "turn around" when the player turns around using the controls.
var rotationSmoothing = 10.0;
// The current move direction in x-y. This will always been (1,0,0) or (-1,0,0)
// The next line, @System.NonSerialized , tells Unity to not serialize the variable or show it in the inspector view. Very handy for organization!
@System.NonSerialized
var direction = Vector3.zero;
// The current vertical speed
@System.NonSerialized
var verticalSpeed = 0.0;
// The current movement speed. This gets smoothed by speedSmoothing.
@System.NonSerialized
var speed = 0.0;
// Is the user pressing the left or right movement keys?
@System.NonSerialized
var isMoving = false;
// The last collision flags returned from controller.Move
@System.NonSerialized
var collisionFlags : CollisionFlags;
// We will keep track of an approximation of the character's current velocity, so that we return it from GetVelocity () for our camera to use for prediction.
@System.NonSerialized
var velocity : Vector3;
// This keeps track of our current velocity while we're not grounded?
@System.NonSerialized
var inAirVelocity = Vector3.zero;
// This will keep track of how long we have we been in the air (not grounded)
@System.NonSerialized
var hangTime = 0.0;
}
var movement : BoyControllerMovement;
private var controller : CharacterController;
// Moving platform support.
private var activePlatform : Transform;
private var activeLocalPlatformPoint : Vector3;
private var activeGlobalPlatformPoint : Vector3;
private var lastPlatformVelocity : Vector3;
// This is used to keep track of special effects in UpdateEffects ();
private var areEmittersOn = false;
function Awake () {
movement.direction = transform.TransformDirection (Vector3.forward);
controller = GetComponent (CharacterController);
Spawn ();
}
function Spawn () {
// reset the character's speed
movement.verticalSpeed = 0.0;
movement.speed = 0.0;
// reset the character's position to the spawnPoint
transform.position = spawnPoint.position;
}
function OnDeath () {
Spawn ();
}
function UpdateSmoothedMovementDirection () {
var h = Input.GetAxisRaw ("Horizontal");
if (!canControl)
h = 0.0;
movement.isMoving = Mathf.Abs (h) > 0.1;
if (movement.isMoving)
movement.direction = Vector3 (h, 0, 0);
// Grounded controls
if (controller.isGrounded) {
// Smooth the speed based on the current target direction
var curSmooth = movement.speedSmoothing * Time.deltaTime;
// Choose target speed
var targetSpeed = Mathf.Min (Mathf.Abs(h), 1.0);
// Pick speed modifier
targetSpeed *= movement.walkSpeed;
movement.speed = Mathf.Lerp (movement.speed, targetSpeed, curSmooth);
movement.hangTime = 0.0;
}
else {
// In air controls
movement.hangTime += Time.deltaTime;
if (movement.isMoving)
movement.inAirVelocity += Vector3 (Mathf.Sign(h), 0, 0) * Time.deltaTime * movement.inAirControlAcceleration;
}
}
function FixedUpdate () {
if (ControlCamSwitch.player1Active == true) {
transform.position.z = 0;
}
}
function ApplyGravity () {
// Apply gravity
var jumpButton = Input.GetButton ("Action");
if (!canControl)
jumpButton = false;
if (controller.isGrounded)
movement.verticalSpeed = -movement.gravity * Time.deltaTime;
else
movement.verticalSpeed -= movement.gravity * Time.deltaTime;
// Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
movement.verticalSpeed = Mathf.Max (movement.verticalSpeed, -movement.maxFallSpeed);
}
function UpdateEffects () {
wereEmittersOn = areEmittersOn;
// By comparing the previous value of areEmittersOn to the new one, we will only update the particle emitters when needed
if (wereEmittersOn != areEmittersOn) {
for (var emitter in GetComponentsInChildren (ParticleEmitter)) {
emitter.emit = areEmittersOn;
}
}
}
function Update () {
if (BoyDance.dancing == true){
canControl = false;}
else {canControl = true;}
UpdateSmoothedMovementDirection();
// Apply gravity
ApplyGravity ();
// Moving platform support
if (activePlatform != null) {
var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
var moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
transform.position = transform.position + moveDistance;
lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime;
} else {
lastPlatformVelocity = Vector3.zero;
}
activePlatform = null;
// Save lastPosition for velocity calculation.
lastPosition = transform.position;
// Calculate actual motion
var currentMovementOffset = movement.direction * movement.speed + Vector3 (0, movement.verticalSpeed, 0) + movement.inAirVelocity;
// We always want the movement to be framerate independent. Multiplying by Time.deltaTime does this.
currentMovementOffset *= Time.deltaTime;
// Move our character!
movement.collisionFlags = controller.Move (currentMovementOffset);
// Calculate the velocity based on the current and previous position.
// This means our velocity will only be the amount the character actually moved as a result of collisions.
movement.velocity = (transform.position - lastPosition) / Time.deltaTime;
// Moving platforms support
if (activePlatform != null) {
activeGlobalPlatformPoint = transform.position;
activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
}
// Set rotation to the move direction
if (movement.direction.sqrMagnitude > 0.01)
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime * movement.rotationSmoothing);
// Update special effects like rocket pack particle effects
UpdateEffects ();
//Make music happen, play animation
// if (Input.GetButtonDown("Action")){
}
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if (hit.moveDirection.y > 0.01)
return;
// Make sure we are really standing on a straight platform
// Not on the underside of one and not falling down from it either!
if (hit.moveDirection.y < -0.9 && hit.normal.y > 0.9) {
activePlatform = hit.collider.transform;
}
}
// Various helper functions below:
function GetSpeed () {
return movement.speed;
}
function GetVelocity () {
return movement.velocity;
}
function IsMoving () {
return movement.isMoving;
}
function IsTouchingCeiling () {
return (movement.collisionFlags & CollisionFlags.CollidedAbove) != 0;
}
function GetDirection () {
return movement.direction;
}
function GetHangTime() {
return movement.hangTime;
}
function Reset () {
gameObject.tag = "Player";
}
function SetControllable (controllable : boolean) {
canControl = controllable;
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterController)
@script AddComponentMenu ("2D Platformer/Platformer Controller")