Question by
LoPiL · Aug 17, 2017 at 11:59 AM ·
2d2d game2d-platformer
Fix soooo hight jump
In some places in scene i can jump more than limit (jumpSpeed) how fix it? https://puu.sh/xcktN/6ae9cc37d9.mp4
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovment : MonoBehaviour {
// public float maxJumpSpeed = 3;
public float maxSpeed = 3;
public float speed = 50f;
public float jumpSpeed = 250f;
private Rigidbody2D rb2d;
public bool grounded;
public Transform groundCheckPoint;
public float groundCheckRadius;
public LayerMask groundLayer;
// Use this for initialization
void Start () {
rb2d = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
grounded = Physics2D.OverlapCircle(groundCheckPoint.position,groundCheckRadius, groundLayer);
if (Input.GetAxis("Horizontal") < -0.1f)
{
transform.localScale = new Vector3(-1, 1, 1);
}
if (Input.GetAxis("Horizontal") > 0.1f)
{
transform.localScale = new Vector3(1, 1, 1);
}
}
void FixedUpdate () {
float h = Input.GetAxis ("Horizontal");
//float w = Input.GetAxis("Vertical");
rb2d.AddForce(Vector2.right * speed * h);
Vector3 easeVelocity = rb2d.velocity;
easeVelocity.y = rb2d.velocity.y;
easeVelocity.z = 0.0f;
easeVelocity.x = 0.75f;
if (grounded)
{
easeVelocity = rb2d.velocity;
}
if (rb2d.velocity.x > maxSpeed)
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}
if (rb2d.velocity.x < -maxSpeed)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
if (Input.GetKeyDown(KeyCode.Z) && grounded)
{
rb2d.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Force);
}
}
}
Comment
Best Answer
Answer by LoPiL · Sep 01, 2017 at 01:52 PM
Edit: To fix it place
if (Input.GetKeyDown(KeyCode.Z) && grounded)
{
rb2d.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Force);
}
in the void Update()