Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 antonsimmerle · Mar 01 at 06:50 PM · gameobjectjumpgame developmentfloating

Player floats in air when jumping

Hey guys, Every time when I jump with my player, he jumps, but lands a little bit above the ground. Does someone know how to fix this?

Here is the movement script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class MenschMovement : MonoBehaviour {

public CharacterController controller; public Transform groundCheck; public LayerMask groundMask; public float groundDistance = 20f; public float jump = 300f; public float speed = 2500f; public float jumpveloctiy = -2500f;

private Vector3 velocity; private bool isGrounded; private float gravity = -5000f;

 // Start is called before the first frame update
 void Start()
 {

 }

 // Update is called once per frame
 void FixedUpdate()
 {

   isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

   if(isGrounded && velocity.y < 0)
   {
     velocity.y = jumpveloctiy;
   }


   if (Input.GetKeyDown(KeyCode.LeftShift))
   {
        speed = speed * 2;
   }

   if (Input.GetKeyUp(KeyCode.LeftShift))
   {
        speed = 2500f;
   }

     float x = Input.GetAxisRaw("Horizontal");
     float z = Input.GetAxisRaw("Vertical");

     Vector3 move = transform.right * x + transform.forward * z;

     controller.Move(move * speed * Time.deltaTime);

     if (Input.GetButtonDown("Jump") && (gameObject.transform.position.y <= 500f))
     {
       velocity.y = Mathf.Sqrt(jump * -200.0f * gravity);
     }

     velocity.y += gravity * Time.deltaTime;
     controller.Move(velocity * Time.deltaTime);


 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by metalted · Mar 01 at 09:19 PM

Usually when this kind of stuff happens, its something to do with the IsGrounded check. For example, a simple check could be to create a raycast pointing downwards to the ground. If the raycast hits the ground, the player should stop falling. But this will also have the effect, that if the raycast is to long, the player will stop in the air, because the raycast hit will return true, before the player model is hitting the ground. Other way around, if the raycast is to short, the player will fall through the floor and then the raycast will hit, stopping the player on the knees. If you have a single point for the center of your player and the origin of your raycast, you have to make sure that your raycast is half the length of your player, so that ray end lines up with the edge of your model.


Instead of a raycast your are using a sphere cast, which is basically the same but will check in a volume. Or see it as an infinite amount of raycast pointing in any possible direction from the origin. The same idea applies. You have to make sure that the radius of your sphere is the same length as half of your player, so that the edges line up. I would give that a try.


This is more of an opinion, but are you sure SphereCheck is the way to go? If you just want to check if the ground is below you, a regular raycast will achieve the same thing. The spherecheck might get you in to situations later where you are detecting objects that are not below you but around you, giving you a false grounded condition. Although you are using a layermask so it should be fine. Food for thought.

Comment
Add comment · Share
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

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

198 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 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

how to solve cannot save with playerPrefts in mycode?? 1 Answer

Player Character looses jump height the further right they go 1 Answer

Cannot Send Class Object From One Scene to Another 1 Answer

iTween gradual jump 2D 0 Answers

How to make my selector print the name of the scriptableobject when the selector is over scriptableobject? 0 Answers


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