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 MadJoneck · Jul 21, 2021 at 08:22 AM · movementplayerbeginnerplatformercontrols

How can I make my characters movement better?

Hi I made a script for basic movement and it works however I do not like how my character jumps because it really fast goes up and then falls for much longer time. Please could you tell me what can I chancge to fix it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class movement : MonoBehaviour
 {
     public float moveSpeed = 5f;
     public float jumpForce = 10f;
     public float gravityModifier = 10f;
 
 
     public float jumpTimeTimer = 0.35f;
     public float maxJumpTime = 0.35f;
 
     
     private float horizontalDirection;
 
     private bool onGround;
     private bool isJumping = false;
     public LayerMask GroundMask;
     public Transform feet;
 
 
     
     Rigidbody2D rb;
     CircleCollider2D circle;
 
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         circle = GetComponent<CircleCollider2D>();
     }
 
     void Move()
     {
         horizontalDirection = Input.GetAxisRaw("Horizontal");
         rb.velocity = new Vector2(horizontalDirection,0)*moveSpeed;        
     }
 
     void Jump()
     {  
         if(isJumping && onGround)
         {
             rb.velocity += Vector2.up * jumpForce;
         }
 
         if(isJumping)
         {
             rb.velocity += Vector2.up * jumpForce;
         }
 
         if(rb.velocity.y<0)
         {
             rb.velocity += Vector2.up * Physics2D.gravity.y * (gravityModifier-1) * Time.deltaTime;
         }
     }
 
     void JumpDetection()
     {
         if(Input.GetKeyDown(KeyCode.Space)&& onGround)
             isJumping = true;
 
         if(Input.GetKey(KeyCode.Space) && isJumping)
             if(jumpTimeTimer>0)
             {
                 isJumping = true;
                 jumpTimeTimer -= Time.deltaTime;
             }else{
                 isJumping = false;
             }
 
         if(Input.GetKeyUp(KeyCode.Space))
             isJumping = false;
     }
 
 
     void GroundCheck()
     {
         onGround = Physics2D.OverlapCircle(feet.position,0.1f,GroundMask);
 
         if(onGround)
             jumpTimeTimer = maxJumpTime;
     }
 
     void FixedUpdate()
     {
         Move(); 
         Jump();
     }
 
     void Update()
     {
         JumpDetection();
         GroundCheck();
     }
 }
 

alt text

And those are my Rigidbody settings.

rigidbody2d.png (38.1 kB)
Comment
Add comment · Show 3
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
avatar image rage_co · Jul 21, 2021 at 10:05 AM 0
Share

I think it's the gravity modifier you implemented, try removing it for once or just generally tweaking it's values....it could also be the scale of your player, try shrinking or enlarging the player

avatar image MadJoneck · Jul 22, 2021 at 07:47 AM 0
Share

okay I found the problem it shouldn't be if(rb.velocity.y < 0) but if(rb.velocity.y < 0.000001f). Now it works.

avatar image rage_co MadJoneck · Jul 22, 2021 at 07:54 AM 0
Share

yeah i guess the value never really goes below zero....im glad you figured this out...you should convert your comment to an answer and accept it to effectively close this question now

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MadJoneck · Jul 22, 2021 at 08:07 AM

The problem was that in my code it shouldn't be if(rb.velocity.y < 0) but if(rb.velocity.y < 0.000001f). Then it works.

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

216 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 avatar image avatar image avatar image avatar image avatar image avatar image 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

Why do I double jump? This isn't supposed to happen... 2 Answers

I cant figure out how to change my simple Movement controls to touch, can anyone help? 1 Answer

Audio for movement 1 Answer

How do I script movement for my player? 1 Answer

Mouse Orbit Script with mecanim & turning character 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