- Home /
Player sticks to ground occasionally after trying to jump
Hello all!
I've been trying to make it so that my Rigidbody player jump correctly while still keeping all of the correct checks in place. My issue is that occasionally the player will get stuck when hitting the "Jump" button. When I inspect the player when this happens, it will show that both the "onGround" and "firstJump" checks as true even though the player is already touching the "Jumpable" surface. The main character also has the ability to hover with a gameobject that follows him around (like Sonic & Tails or DKC) which I don't think should be the problem, but just in case I added it in the complete script I've made. I may just have some booleans not placed correctly, or I could be going the completely wrong direction in this. I'm just getting back into scripting and Unity so any help would be appreciated!!!
Thanks for your time guys. Please let me know if I need to explain or include anything else!
//Character state variables
var isLoki : boolean = true;
var isHoodie : boolean = false;
//Object Variables
var inactiveChar : GameObject;
//Material variables
var matArray : Material[];
//Movement variables
var speed : float = 6.0;
var maxSpeed : float = 10.0f;
var jumpHeight : float = 8.0;
var onGround : boolean;
var firstJump : boolean;
private var jumped : boolean;
private var moveRight : boolean;
private var moveLeft : boolean;
private var hoverRight : boolean;
private var hoverLeft : boolean;
private var hoverTimerEnd : boolean;
//Hovering variables
public var isHovering: boolean;
var hoverSpeed: float = 3.0;
var maxHoverTime: float = 2.0;
private var currentHoverTime: float;
//Offset variables
var inactivePlayerOffset : float = -1.5;
private var relativePositionOffset : Vector3 = Vector3(0,0,0);
function Start() {
//Setting hover timer to 0
currentHoverTime = 0;
//Instantiates inactive character
Instantiate (inactiveChar, transform.position + relativePositionOffset+(transform.right*inactivePlayerOffset), Quaternion.identity);
}
function FixedUpdate() {
//Trying to limit speed
if (GetComponent.<Rigidbody>().velocity.magnitude > maxSpeed){
GetComponent.<Rigidbody>().velocity = Vector3.ClampMagnitude(GetComponent.<Rigidbody>().velocity, maxSpeed);
}
//Right/left movement (physics)
if (moveRight == true) {
GetComponent.<Rigidbody>().velocity.x = speed;
}
if (moveLeft == true) {
GetComponent.<Rigidbody>().velocity.x = -speed;
}
//Hover movement (physics)
if (hoverRight == true) {
GetComponent.<Rigidbody>().velocity.x = hoverSpeed;
}
if (hoverLeft == true) {
GetComponent.<Rigidbody>().velocity.x = -hoverSpeed;
}
//Jump (physics)
if (jumped == true) {
GetComponent.<Rigidbody>().velocity.y = jumpHeight;
}
//Hover constraints
if (isHovering == true) {
GetComponent.<Rigidbody>().useGravity = false;
GetComponent.<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
}
if (hoverTimerEnd == true) {
GetComponent.<Rigidbody>().useGravity = true;
GetComponent.<Rigidbody>().constraints = ~RigidbodyConstraints.FreezePositionY;
GetComponent.<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
hoverTimerEnd = false;
}
}
function Update() {
/*//Loki moveset
if (isLoki == true && !isHoodie) {
}
//Hoddie moveset
if (isLoki == true && !isHoodie) {
}*/
//Hover Timer
if (!onGround && isHovering == true){
currentHoverTime += Time.deltaTime;
}
else {
currentHoverTime = 0;
}
//Normal Movement
if (Input.GetButton ("Right") && !isHovering) {
moveRight = true;
}
else {
moveRight = false;
}
if (Input.GetButton ("Left") && !isHovering) {
moveLeft = true;
}
else {
moveLeft = false;
}
//Hover movement
if (Input.GetButton ("Right") && !onGround && isHovering == true) {
hoverRight = true;
}
else {
hoverRight = false;
}
if (Input.GetButton ("Left") && !onGround && isHovering == true) {
hoverLeft = true;
}
else {
hoverLeft = false;
}
//BUG: Player uses jump right as they hit the ground, onGround and firstJump are true? if player not on ground && first jump == true (don't regular jump)
//Jump
if (Input.GetButtonDown ("Jump") && onGround == true) {
jumped = true;
firstJump = true;
}
else {
jumped = false;
}
//Hover
if (Input.GetButtonDown ("Jump") && onGround == false && firstJump == true) {
firstJump = false;
isHovering = true;
}
//Hover timer
if (currentHoverTime > maxHoverTime) {
isHovering = false;
hoverTimerEnd = true;
}
// Character/material switch
if (Input.GetButtonDown ("Switch")) {
if (isHoodie == false && isLoki == true) {
isHoodie = true;
isLoki = false;
GetComponent.<Renderer>().material = matArray[0];
}
else {
if (isHoodie == true && isLoki == false) {
isHoodie = false;
isLoki = true;
GetComponent.<Renderer>().material = matArray[1];
}
}
}
}
function OnCollisionEnter(collision : Collision){
if (collision.collider.gameObject.layer == LayerMask.NameToLayer("Jumpable")) {
firstJump = false;
onGround = true;
isHovering = false;
currentHoverTime = 0;
//Debug.Log("Jumpable!");
}
}
function OnCollisionExit(collision : Collision) {
if (collision.collider.gameObject.layer == LayerMask.NameToLayer("Jumpable")) {
onGround = false;
//Debug.Log("Not jumpable!");
}
}
Your answer
Follow this Question
Related Questions
Player wont jump (visual script),2d player does not jump (visual script) 1 Answer
Box collider didn't jump with player 1 Answer
how to jump with 2d? 1 Answer
Detecting Collision for Jumping 1 Answer
Character Fails To Jump Sometimes 1 Answer