- Home /
Character teleports to the apex of full jump height
I'm having an issue where my character will teleport to the apex of a jump when I hit the jump button. It pretty much goes to the full jump height as soon as the button is pressed, though sometimes it will just teleport to a short distance off the ground. Any ideas why this is happening and how I can fix it?
Here's the script:
var walkSpeed = 6.0;
var runSpeed = 11.0;
var limitDiagonalSpeed = true;
var toggleRun = false;
var jumpSpeed = 8.0;
var gravity = 20.0;
var fallingDamageThreshold = 10.0;
var slideWhenOverSlopeLimit = false;
var slideOnTaggedObjects = false;
var slideSpeed = 12.0;
var airControl = false;
var antiBumpFactor = .75;
var antiBunnyHopFactor = 1;
private var moveDirection = Vector3.zero;
private var grounded = false;
private var controller : CharacterController;
private var myTransform : Transform;
private var speed : float;
private var hit : RaycastHit;
private var fallStartLevel : float;
private var falling = false;
private var slideLimit : float;
private var rayDistance : float;
private var contactPoint : Vector3;
private var playerControl = false;
private var jumpTimer : int;
private var mouseSideButton:boolean = false; //temp var for mouse side buttons
private var pbuffer:float = 0.0; //Cooldownpuffer for SideButtons
private var coolDown:float = 0.5; //Cooldowntime for SideButtons
private var rotateSpeed:float = 250.0; //Rotationspeed of the Character
public var swimming:boolean = false; //Can be triggert to slow down the movements (like when u swim)
public var moveStatus:String = "idle"; //movestatus for animations
private var walkBackMod:float = 0.75; //Speed in Percent for walk backwards and sidewalk
private var speedMod:float = 0.0; //temp Var for Speedcalculation
private var isWalking:boolean = false; //toggle var between move and run
private var jumping:boolean = false; //temp var for jumping
function Start () {
controller = GetComponent(CharacterController);
myTransform = transform;
speed = walkSpeed;
rayDistance = controller.height * .5 + controller.radius;
slideLimit = controller.slopeLimit - .1;
jumpTimer = antiBunnyHopFactor;
oldPos = transform.position;
}
function FixedUpdate() {
var inputX = Input.GetAxis("Horizontal");
var inputY = Input.GetAxis("Vertical");
// If both horizontal and vertical are used simultaneously, limit speed (if allowed), so the total doesn't exceed normal move speed
var inputModifyFactor = (inputX != 0.0 && inputY != 0.0 && limitDiagonalSpeed)? .7071 : 1.0;
if (grounded) {
var sliding = false;
// See if surface immediately below should be slid down. We use this normally rather than a ControllerColliderHit point,
// because that interferes with step climbing amongst other annoyances
if (Physics.Raycast(myTransform.position, -Vector3.up, hit, rayDistance)) {
if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
sliding = true;
}
// However, just raycasting straight down from the center can fail when on steep slopes
// So if the above raycast didn't catch anything, raycast down from the stored ControllerColliderHit point instead
else {
Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, hit);
if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
sliding = true;
}
// If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down
if (!toggleRun)
speed = Input.GetButton("Run")? runSpeed : walkSpeed;
// If sliding (and it's allowed), or if we're on an object tagged "Slide", get a vector pointing down the slope we're on
if ( (sliding && slideWhenOverSlopeLimit) || (slideOnTaggedObjects && hit.collider.tag == "Slide") ) {
var hitNormal = hit.normal;
moveDirection = Vector3(hitNormal.x, -hitNormal.y, hitNormal.z);
Vector3.OrthoNormalize (hitNormal, moveDirection);
moveDirection *= slideSpeed;
playerControl = false;
}
// Otherwise recalculate moveDirection directly from axes, adding a bit of -y to avoid bumping down inclines
else {
moveDirection = Vector3(inputX * inputModifyFactor, -antiBumpFactor, inputY * inputModifyFactor);
moveDirection = myTransform.TransformDirection(moveDirection) * speed;
playerControl = true;
}
}
else {
// If we stepped over a cliff or something, set the height at which we started falling
if (!falling) {
falling = true;
fallStartLevel = myTransform.position.y;
}
// If air control is allowed, check movement but don't touch the y component
if (airControl && playerControl) {
moveDirection.x = inputX * speed * inputModifyFactor;
moveDirection.z = inputY * speed * inputModifyFactor;
moveDirection = myTransform.TransformDirection(moveDirection);
}
}
// Move the controller, and set grounded true or false depending on whether we're standing on something
grounded = (controller.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0;
}
function Update () {
// If the run button is set to toggle, then switch between walk/run speed. (We use Update for this...
// FixedUpdate is a poor place to use GetButtonDown, since it doesn't necessarily run every frame and can miss the event)
if (toggleRun && grounded && Input.GetButtonDown("Run"))
speed = (speed == walkSpeed? runSpeed : walkSpeed);
//movedirection
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
if (grounded){
// Jump! But only if the jump button has been released and player has been grounded for a given number of frames
if (!Input.GetButton("Jump"))
jumpTimer++;
else if (jumpTimer >= antiBunnyHopFactor) {
moveDirection.y = jumpSpeed;
jumpTimer = 0;
jumping = true;
}
}
else {
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
}
//pushbuffer to avoid on/off flipping
if(pbuffer>0)
pbuffer -=Time.deltaTime;
if(pbuffer<0)pbuffer=0;
//Automove Sidebuttonmovement
if(Input.GetAxis("Toggle Move") && pbuffer == 0){
pbuffer=coolDown;
mouseSideButton = !mouseSideButton;
}
if(mouseSideButton && ((Input.GetAxis("Vertical") != 0) || Input.GetButton("Jump") || (Input.GetMouseButton(0) && Input.GetMouseButton(1)) ))
mouseSideButton = false;
//L+R MouseButton Movement
if (Input.GetMouseButton(0) && Input.GetMouseButton(1) || mouseSideButton)
moveDirection.z += 1;
if (moveDirection.z > 1)
moveDirection.z = 1;
//Strafing move (like Q/E movement
moveDirection.x -= Input.GetAxis("Strafing");
// if moving forward and to the side at the same time, compensate for distance
if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) {
moveDirection *= .7;
}
//Speedmodification / is moving forward or side/backward
speedMod = ((Input.GetAxis("Vertical") < 0) || (Input.GetMouseButton(1) && Input.GetAxis("Horizontal")) || Input.GetAxis("Strafing") != 0) ? walkBackMod : 1.0;
//Use run or walkspeed
moveDirection *= isWalking ? walkSpeed * speedMod : runSpeed * speedMod;
//reduce movement by 70% when swimming is toggled
moveDirection*= swimming ? 0.7 : 1;
//transform direction
moveDirection = transform.TransformDirection(moveDirection);
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(1)) {
transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
} else {
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
//Reset jumping after landing
jumping = grounded ? false : jumping;
}
@script RequireComponent(CharacterController)
I don't know if you solved this already but maybe your own "grounded" variable is a problem. You should use if(controller.isGrounded) which is unity's own boolean if a charactercontroller is grounded. And you should apply gravity all the time, not just when not grounded. Hope this helps
Your answer
Follow this Question
Related Questions
"Teleport" in the way the player is heading 2 Answers
Gravity and Jumping for a Character Controller 1 Answer
Translation of an object 2 Answers
Impact Sound When Jumping? (JavaScript) 1 Answer
Jump Script ow to make it??? 2 Answers