- Home /
how can I make the character jump more by holding the jump button in this script?
hello, I started recently in the world of unity and they gave me this script, I've been 2 months trying to make the character jump more when I keep pressing the jump button, unfortunately I haven't got any result, here is the code if someone can help me :C
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour
{
public class GroundState
{
private GameObject player;
private float width;
private float height;
private float length;
//GroundState constructor. Sets offsets for raycasting.
public GroundState(GameObject playerRef)
{
player = playerRef;
width = player.GetComponent<Collider2D>().bounds.extents.x + 0.1f;
height = player.GetComponent<Collider2D>().bounds.extents.y + 0.2f;
length = 0.05f;
}
//Returns whether or not player is touching wall.
public bool isWall()
{
bool left = Physics2D.Raycast(new Vector2(player.transform.position.x - width, player.transform.position.y), -Vector2.right, length);
bool right = Physics2D.Raycast(new Vector2(player.transform.position.x + width, player.transform.position.y), Vector2.right, length);
if (left || right)
return true;
else
return false;
}
//Returns whether or not player is touching ground.
public bool isGround()
{
bool bottom1 = Physics2D.Raycast(new Vector2(player.transform.position.x, player.transform.position.y - height), -Vector2.up, length);
bool bottom2 = Physics2D.Raycast(new Vector2(player.transform.position.x + (width - 0.2f), player.transform.position.y - height), -Vector2.up, length);
bool bottom3 = Physics2D.Raycast(new Vector2(player.transform.position.x - (width - 0.2f), player.transform.position.y - height), -Vector2.up, length);
if (bottom1 || bottom2 || bottom3)
return true;
else
return false;
}
//Returns whether or not player is touching wall or ground.
public bool isTouching()
{
if (isGround() || isWall())
return true;
else
return false;
}
//Returns direction of wall.
public int wallDirection()
{
bool left = Physics2D.Raycast(new Vector2(player.transform.position.x - width, player.transform.position.y), -Vector2.right, length);
bool right = Physics2D.Raycast(new Vector2(player.transform.position.x + width, player.transform.position.y), Vector2.right, length);
if (left)
return -1;
else if (right)
return 1;
else
return 0;
}
}
//Feel free to tweak these values in the inspector to perfection. I prefer them private.
public float speed = 14f;
public float accel = 6f;
public float airAccel = 3f;
public float jump = 14f; //I could use the "speed" variable, but this is only coincidental in my case. Replace line 89 if you think otherwise.
private GroundState groundState;
void Start()
{
//Create an object to check if player is grounded or touching wall
groundState = new GroundState(transform.gameObject);
}
private Vector2 input;
void Update()
{
//Handle input
if (Input.GetKey(KeyCode.LeftArrow))
input.x = -1;
else if (Input.GetKey(KeyCode.RightArrow))
input.x = 1;
else
input.x = 0;
if (Input.GetKeyDown(KeyCode.UpArrow))
input.y = 1;
//Reverse player if going different direction
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, (input.x == 0) ? transform.localEulerAngles.y : (input.x + 1) * 90, transform.localEulerAngles.z);
}
void FixedUpdate()
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(((input.x * speed) - GetComponent<Rigidbody2D>().velocity.x) * (groundState.isGround() ? accel : airAccel), 0)); //Move player.
GetComponent<Rigidbody2D>().velocity = new Vector2((input.x == 0 && groundState.isGround()) ? 0 : GetComponent<Rigidbody2D>().velocity.x, (input.y == 1 && groundState.isTouching()) ? jump : GetComponent<Rigidbody2D>().velocity.y); //Stop player if input.x is 0 (and grounded) and jump if input.y is 1
if (groundState.isWall() && !groundState.isGround() && input.y == 1)
GetComponent<Rigidbody2D>().velocity = new Vector2(-groundState.wallDirection() * speed * 0.75f, GetComponent<Rigidbody2D>().velocity.y); //Add force negative to wall direction (with speed reduction)
input.y = 0;
}
}
Answer by CodesCove · Nov 07, 2020 at 10:09 PM
I'll try..
First of all, you better of trying to have some more simpler example script + check some movement tutorials that you can examine and understand yourself. There is too much happening in this script for beginner and some of the stuff far is from ideal (ie. should cache the Rigidbody reference)...
I really didn't go trough the whole code and I haven't tested this solution.. just to let you know :)
I also don't know what you exactly mean by "jumping more" but anyway replace this
GetComponent<Rigidbody2D>().velocity = new Vector2((input.x == 0 && groundState.isGround()) ? 0 : GetComponent<Rigidbody2D>().velocity.x, (input.y == 1 && groundState.isTouching()) ? jump : GetComponent<Rigidbody2D>().velocity.y);
with this
GetComponent<Rigidbody2D>().velocity = new Vector2((input.x == 0 && groundState.isGround()) ? 0 : GetComponent<Rigidbody2D>().velocity.x, (input.y == 1) ? jump : GetComponent<Rigidbody2D>().velocity.y);
It should now keep on jumping (setting upward velocity) even if its not grounded .. and once again.. I didn't test this but idea should be ok and maybe you get more idea how to get it working yourself (or even make it better).
Oh.. and just out of curiosity, who is "they" that gave you this code?
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Jumping while moving 1 Answer
Problem with Jump 1 Answer
Not able to jump when running!Where did I go wrong? 1 Answer