Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rockerphobia · Oct 11, 2017 at 03:52 PM · javascriptcollisionjumpjumpingplayer movement

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!");
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

150 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges