- Home /
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();
}
}
And those are my Rigidbody settings.
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
okay I found the problem it shouldn't be if(rb.velocity.y < 0) but if(rb.velocity.y < 0.000001f). Now it works.
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
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.
Your answer
Follow this Question
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
How do I set up my players controller script (How do I change the controls used to move and look) 2 Answers