Edit: A new script was created to replace the Third Person controller, based off of the original controller. The player is not moving…
Edit: I found something out using the Lerpz Third Person Controller… The floating happens only after collision with the enemy. I ended up starting a game, colliding with the enemy several times, and then having the player float upward with all movements!
Gravity is giving me problems! I have the main character (“Player”) in my game. He normally does what he should do when I control him, but at random times, he quickly floats upward. He eventually falls back downward, but not after leaving the room I have made through the ceiling.
I have attached the Character Controller (from Unity) and a custom script for controlling the player. The code for the script is as follows:
//Set variables!
var runSpeed : float;
var gravity : float = 20;
var addedGravity : float = 3;
var rotateSpeed : float;
var speedSmoothing : float;
private var moveDirection : Vector3 = Vector3.zero;
var verticalSpeed : float = 0;
var moveSpeed : float = 0;
private var collisionFlags : CollisionFlags;
private var lockCameraTimer : float = 0;
private var movingBack : boolean = false;
private var isMoving : boolean = false;
function Awake (){
moveDirection = transform.TransformDirection(Vector3.forward);
}
function UpdateSmoothedMovementDirection(){
var cameraTransform : Transform = Camera.main.transform;
var grounded = IsGrounded();
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
var right : Vector3 = Vector3(forward.z,0,-forward.x);
//Inputs
var v = Input.GetAxisRaw("Vertical");
var h = Input.GetAxisRaw("Horizontal");
right = Vector3(forward.z, 0, -forward.x);
var left = -right;
if(v< -0.2){
movingBack = true;
}
else{
movingBack = false;
}
var wasMoving : boolean = isMoving;
isMoving = Mathf.Abs(h) > 0.1 || Mathf.Abs(v)>0.1;
var targetDirection = h * right + v * left;
if(grounded){
if (isMoving != wasMoving)
lockCameraTimer += Time.deltaTime;
if (targetDirection != Vector3.zero){
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
var curSmooth = speedSmoothing * Time.deltaTime;
var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
}
}
function Update(){
//create variable for the controller
var controller : CharacterController;
controller = GetComponent(CharacterController);
//I know what to do here! Gravity!
moveDirection[1] -= gravity * Time.deltaTime;
UpdateSmoothedMovementDirection();
var movement = moveDirection * moveSpeed;
movement *= Time.deltaTime;
collisionFlags = controller.Move(movement);
if(IsGrounded()){
transform.rotation = Quaternion.LookRotation(moveDirection);
}
else{
moveDirection[1] -= gravity * Time.deltaTime * addedGravity;
}
}
function GetSpeed () {
return moveSpeed;
}
function IsGrounded () {
return (collisionFlags & CollisionFlags.CollidedBelow) != 0;
}
function GetDirection () {
return moveDirection;
}
function IsMovingBackwards () {
return movingBack;
}
function GetLockCameraTimer (){
return lockCameraTimer;
}
function IsMoving () : boolean {
return Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5;
}
function Reset (){
gameObject.tag = "Player";
}
@script RequireComponent(CharacterController)
@script AddComponentMenu("Third Person Player/Main M Controller")
Does anyone have any suggestions on fixing this?
Edit: Here is the Player Damage script, which contains a collision event with the enemy.
private var canDamage : boolean = true;
public var invincibletime : float = 2.9;
function OnTriggerEnter(Enemy : Collider) { if(Enemy.gameObject.tag == “MarshmellowMan”)
{
if(canDamage)
{
ApplyDamage();
}
else
{
return;
}
}
}
function ApplyDamage() {
PlayerStat.health -= 1;
canDamage = false;
yield WaitForSeconds(invincibletime);
canDamage = true;
}