Player has little control over the character after dashing in midair - c#

I'm working on a 2.5D player controller right now, and there is a problem with my dash in midair. The dash works, but the character can't be completely controlled on the X axis after the dash is finished until they hit the ground. I want the player to have full control on the X axis right after the dash so that they can dodge appropriately.
void Update()
{
PlayerInput();
}
void FixedUpdate()
{
xMovement();
yMovement();
}
void PlayerInput()
{
//Registers X and Y movement
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
if (Input.GetButton("Run"))
{
isRunning = true;
}
else if (Input.GetButtonUp("Run"))
{
isRunning = false;
}
//Makes player jump by returning a bool value to "yMovement()" when pressed.
if (Input.GetButtonDown("Jump"))
{
jumpRequest = true;
}
if (Input.GetKeyDown(KeyCode.A))
{
if (doubleTapTime > Time.time && lastKeyCode == KeyCode.A)
{
StartCoroutine(Dash(1f));
Debug.Log("You dashed left");
}
else
{
doubleTapTime = Time.time + 0.5f;
}
lastKeyCode = KeyCode.A;
}
if (Input.GetKeyDown(KeyCode.D))
{
if (doubleTapTime > Time.time && lastKeyCode == KeyCode.D)
{
StartCoroutine(Dash(-1f));
Debug.Log("You dashed right");
}
else
{
doubleTapTime = Time.time + 0.5f;
}
lastKeyCode = KeyCode.D;
}
}
void xMovement()
{
//Makes player walk left and right
if (!isRunning && !isDashing)
{
transform.Translate(Vector3.left * walkSpeed * horizontalInput * Time.deltaTime);
}
//Makes player run left and right
else if (isRunning && !isDashing)
{
transform.Translate(Vector3.left * runSpeed * horizontalInput * Time.deltaTime);
}
}
void yMovement()
{
//Make player jump when Jump is pressed
if (jumpRequest)
{
playerRb.velocity = new Vector3(playerRb.velocity.x, jumpForce);
jumpRequest = false;
//playerRb.velocity = new Vector2(playerRb.velocity.x, playerRb.velocity.y * jumpForce);
}
//Makes player fall faster in general, and when the Jump button is released
if (playerRb.velocity.y < 0)
{
playerRb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplyer - 1) * Time.deltaTime;
}
else if (playerRb.velocity.y > 0 && !Input.GetButton("Jump"))
{
playerRb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplyer - 1) * Time.deltaTime;
}
}
IEnumerator Dash(float direction)
{
isDashing = true;
playerRb.velocity = new Vector3(playerRb.velocity.x, .0f);
playerRb.velocity = new Vector3(dashDistance * direction, 0f, 0f);
playerRb.useGravity = false;
yield return new WaitForSeconds(.2f);
isDashing = false;
playerRb.useGravity = true;
Any tips on code optimization is also greatly appreciated. I'm still fairly new to coding and would rather learn appropriate coding habits before I have to unlearn bad ones. Thank you!

I think your issue is that you're using a Translation on transform for the x axis when on the yAxis you're actually using the velocity. Unity might have trouble dealing with both in a single "FixedUpdate" call. Or it might just not do what you expect.
I would recommend sticking to velocity changes. So that would give something like
void xMovement()
{
//Makes player walk left and right
if (!isRunning && !isDashing)
{
playerRb.velocity += Vector3.left * walkSpeed * horizontalInput * Time.deltaTime;
}
//Makes player run left and right
else if (isRunning && !isDashing)
{
playerRb.velocity += Vector3.left * runSpeed * horizontalInput * Time.deltaTime;
}
}

Related

I'm trying to make a movement script where the character moves from side to side and it just stays to one side of the screen... (Unity 2D)

Here is my code, if it helps anyone...
I don't really know what's wrong with it but when I searched online for an answer nothing helpful came up. It was all player controlled scripts and not anything I could use. Any help would be greatly appreciated, Thanks.
'''
[HideInInspector] public bool moving;
[HideInInspector] public int direction;
public float restTime;
public float speed = 0.1f;
private float maxX = 2f;
private float minX = -2f;
private float currentX;
void Start()
{
SpriteAnimator animator = gameObject.GetComponent<SpriteAnimator>();
currentX = gameObject.transform.position.x;
string message = "Moving: " + moving + ", Dirrection: " + direction + ", Current X: " + currentX;
Debug.Log(message);
}
void Update()
{
if (restTime > 0f)
{
restTime -= Time.deltaTime;
}
else
{
if (direction == 0 && currentX < minX)
{
moving = true;
while (currentX >= minX)
{
currentX = gameObject.transform.position.x;
gameObject.transform.position -= new Vector3(speed * Time.deltaTime, 0f, 0f);
}
moving = false;
restTime = 5f;
}
else
{
moving = true;
while (currentX <= maxX)
{
currentX = gameObject.transform.position.x;
gameObject.transform.position += new Vector3(speed * Time.deltaTime, 0f, 0f);
}
moving = false;
restTime = 5f;
}
}
}
'''
At first glance I see that if (direction == 0 && currentX < minX) should instead be if (direction == 0 && currentX > minX). Another problem that I see is that each frame would see the object be on one side or the other with no frames at positions between the two locations. If this is intended to always run with the object nearly constantly moving back and forth across the screen I would suggest trying this.
IEnumerator MoveObject() {
while(true) {
if(direction == 0) {
while(currentX >= minX) {
gameObject.transform.position -= new Vector3(speed * Time.deltaTime, 0f, 0f);
currentX = gameObject.transform.position.x;
yield return null; // waits for the next frame before continuing
}
direction == 1;
} else {
while(currentX <= maxX) {
gameObject.transform.position += new Vector3(speed * Time.deltaTime, 0f, 0f);
currentX = gameObject.transform.position.x;
yield return null; // waits for the next frame before continuing
}
direction == 0;
}
yield return new WaitForSecondsRealtime(5f); // how long to wait before continuing
// If you want to allow for the "pausing" of the coroutine by modifying the passage of in-game time, you would call
// yield return new WaitForSeconds(5f);
}
}
And you can call it from the Start Method as follows
private void Start() {
// Other Code Here //
_= StartCoroutine(MoveObject());
}

Jump script in unity involving rb.velocity not working

Here is my code and inspector data, I am rookie in unity And I do not have an idea why it is not working. I tagged this as a 3D problem, since I think it wouldn't work in either. I watched some youtube videos and other posts, and I cannot find a mistake.I would be grateful for any help.
This is picture of my inspector
public Animator animator;
public SpriteRenderer sprite;
public Rigidbody2D rb;
Vector2 playerDirection;
public float speed;
public float jumpForce;
public float fallMultiplier=2.5f;
public float lowJumpMultiplier=2f;
void Update()
{
playerDirection.x = Input.GetAxisRaw("Horizontal");
//Animation
////////////////////////////////////////////////////
if (playerDirection.x != 0)
animator.SetBool("isMoving", true);
else
animator.SetBool("isMoving", false);
////////////////////////////////////////////////////
//flip
////////////////////////
if (playerDirection.x < 0)
{
sprite.flipX = true;
}
if (playerDirection.x>0)
{
sprite.flipX = false;
}
////////////////////////
//Jump
///////////////////////
////////////////////////
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + speed *playerDirection * Time.fixedDeltaTime);
if (Input.GetButton("Jump"))
{
rb.velocity = Vector2.up * jumpForce;
}
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * fallMultiplier * Time.fixedDeltaTime;
}
else
if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.W))
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - rb.gravityScale) * Time.fixedDeltaTime;
}
}
First of all you need to understand the difference between Update and FixedUpdate. Update runs at all the frames. FixedUpdate runs at specific time intervals. What you are doing is registering user input in the FixedUpdate. That means that, you need to be lucky and have the FixedUpdate run at the specific frame you pressed the button. If you had pysics calculations, you should indeed place them in the FixedUpdate. However, when jumping, you do not, you simply set the velocity. Now, the falling part is interesting, because you do want that in the FixedUpdate.
Try this:
void Update()
{
playerDirection.x = Input.GetAxisRaw("Horizontal");
//Animation
////////////////////////////////////////////////////
if (playerDirection.x != 0)
animator.SetBool("isMoving", true);
else
animator.SetBool("isMoving", false);
////////////////////////////////////////////////////
//flip
////////////////////////
if (playerDirection.x < 0)
{
sprite.flipX = true;
}
if (playerDirection.x>0)
{
sprite.flipX = false;
}
////////////////////////
//Jump
///////////////////////
if (Input.GetButton("Jump"))
{
Debug.Log("Jumping button registered");
rb.velocity = Vector2.up * jumpForce;
}
////////////////////////
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + speed *playerDirection * Time.fixedDeltaTime);
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * fallMultiplier * Time.fixedDeltaTime;
}
else
if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.W))
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - rb.gravityScale) * Time.fixedDeltaTime;
}
}
Once you make sure that the jump is registered, it should probably work fine. If however you do NOT get console log when pressing the jump button, you should check your input settings.

How to make sliding mechanic gain speed when on a slope

I am a new unity developer that is trying to make a 3d game. So far I have managed to develop a pretty cool first-person character controller. It uses a character controller and it has sliding and jumping mechanics.
And the question is:
How can I make my player lose sliding speed when sliding up a slope and gain sliding speed when sliding down a slope?
Here is my code so far and please keep in mind that my code is messy and that I am a still beginner. If you need any further information please don't hesitate to ask. Thanks for your time! Also, any suggestions are more than welcome!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementScript : MonoBehaviour
{
//Character controller refrence
private CharacterController controller;
//Camera refrence
[SerializeField] Camera fpsCam;
//IsGrounded variables
private float groundDistance = 1.5f;
public bool isGrounded;
//SomethingAbove variables
private float cellingDistance = 1.5f;
public bool somethingAbove;
//Variables
//Speed that is modified
private float speed = 12f;
//Default speed
private float baseSpeed = 12f;
//Slide speed that is modified
private float slideSpeed = 15f;
//Default slide speed
private float slideSpeedStorage = 15f;
//Crouch speed
private float crouchSpeed = 3f;
//Jump height
private float jumpHeight = 2f;
private bool isJumping = false;
//Gravity
private float gravity = -14.715f;
//Air resistance
private float xAirResistance = 0.5f;
private float zAirResistance = 0.35f;
//Air multipiler
private float airMultiplierJump = 1.1f;
//Fov
private float fovWhenSliding = 100f;
private float defaultFov;
//Crouch variables
float originalHeight;
float reducedHeight = 0.5f;
//Player input
float x;
float z;
bool jump;
bool control;
//Directions
Vector3 velocity;
private Vector3 move = new Vector3();
private Vector3 slideDirection = new Vector3();
private Vector3 jumpDirection = new Vector3();
private void Awake()
{
controller = GetComponent<CharacterController>();
}
private void Start()
{
//Getting the original height
originalHeight = controller.height;
//Getting the default fov
defaultFov = fpsCam.fieldOfView;
}
private void Update()
{
MyInput();
Calculations();
PlayerMovement();
}
private void MyInput()
{
//Getting WASD input
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
//Space bar input
jump = Input.GetButtonDown("Jump");
//Left control input
control = Input.GetKey(KeyCode.LeftControl);
}
private void Calculations()
{
//Checking if player is grounded
Debug.DrawRay(transform.position, Vector3.down * groundDistance, Color.red);
isGrounded = Physics.Raycast(transform.position, Vector3.down, groundDistance);
//Checking if player can get up
Debug.DrawRay(transform.position, Vector3.up * cellingDistance, Color.black);
somethingAbove = Physics.Raycast(transform.position, Vector3.up, cellingDistance);
/*//Getting the player has enough speed and getting jump direction
if (isGrounded && jump && z > 0)
{
//If pressing W the jump direction is forward
jumpDirection = transform.forward;
}*/
//Movement in air
if (isGrounded)
{
//On ground movement
move = transform.right * x + transform.forward * z;
}
else
{
//If pressing W
if (z > 0)
{
//Jump direction multiplied by airMultiplierJump
move = transform.forward * z * airMultiplierJump + transform.right * x * xAirResistance;
}
else
{
//Jump direction = Vector3.zero;
move = transform.forward * z * zAirResistance + transform.right * x * xAirResistance;
}
}
//Checking if the player has enough speed for sliding and getting direction
if (Input.GetKeyDown(KeyCode.LeftControl) && isGrounded)
{
if (z == 1 || x == 1 || x == -1)
{
//If pressing W || A || D slide direction is the direction u are moving towards
slideDirection = transform.forward * z + transform.right * x;
}
else
{
//If pressing S the slide direction is zero
slideDirection = Vector3.zero;
}
}
//Adding extra force so the player stays on ground
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
//Calculating gravity
velocity.y += gravity * Time.deltaTime;
//Normalizing the move vector so the diagonal and normal movement are the same speed
if (move.sqrMagnitude > 1)
{
move.Normalize();
}
//Extra force on slope downwards so the player stays on it
if((x != 0 || z != 0) && OnSlope())
{
controller.Move(Vector3.down * controller.height / 2 * 2f);
}
}
private void PlayerMovement()
{
Jump();
Slide();
//Moving the player with WASD
controller.Move(move * speed * Time.deltaTime);
//Applying gravity
controller.Move(velocity * Time.deltaTime);
}
private void Slide()
{
//Checking for left control input and if the player is grounded
if(control && isGrounded && !OnSlope())
{
StartSlide();
}
//Checking if the player can uncrouch
else if(!somethingAbove && isGrounded)
{
StopSlide();
}
else if (!control)
{
controller.height = originalHeight;
speed = baseSpeed;
}
else if(control && !isGrounded)
{
controller.height = originalHeight;
speed = baseSpeed;
}
else
{
controller.height = reducedHeight;
speed = crouchSpeed;
}
}
//Starting to slide
private void StartSlide()
{
if (z != 1 || x != -1 || x != 1)
{
speed = crouchSpeed;
}
else
{
speed = 0f;
}
controller.height = reducedHeight;
controller.Move(slideDirection * Time.deltaTime * slideSpeed);
slideSpeed -= 10f * Time.deltaTime;
if (slideSpeed <= 0)
{
slideSpeed = 0f;
speed = crouchSpeed;
}
}
//Stopping the slide
private void StopSlide()
{
controller.height = originalHeight;
speed = baseSpeed;
//Slide speed recovery
slideSpeed = Mathf.Lerp(slideSpeed, slideSpeedStorage, Time.deltaTime * 2);
}
//Jumping mechanic
private void Jump()
{
if (jump && isGrounded)
{
isJumping = true;
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
private bool OnSlope()
{
if (isJumping)
{
return false;
}
RaycastHit hit;
if(Physics.Raycast(transform.position, Vector3.down, out hit, controller.height / 2 * 5f))
if(hit.normal != Vector3.up)
{
return true;
}
return false;
}
//Stopping the slide if hitting object
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.transform.tag == "SlideStop")
{
slideSpeed = 0f;
speed = crouchSpeed;
}
}
}```
private bool OnSlope()
{
if (isJumping)
{
return false;
}
RaycastHit hit;
if(Physics.Raycast(transform.position, Vector3.down, out hit, controller.height / 2 * 5f))
if(hit.normal != Vector3.up)
{
return true;
}
return false;
}
One issue is comparing Vector3s which is bad practice, since they are arrays of floats. Even if the plane is completely planar, it might still return true due to what's known as 'floating point errors'. A better practice is to use:
Vector3.distance(hit.normal, Vector3.up) > .1f // or some other small value;

Character doesn't jump sometimes - Unity

I am new to Unity and I am using the following CharacterController for my character. Everything is working well, except that sometimes the character jumps and sometimes it doesn't when I hit the spacebar. I used Debog.Log using Raycast to check if my character is grounded, and the result was True. So what is preventing my character from jumping whenever I hit the key?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class RPGMovement : MonoBehaviour
{
public float ForwardSpeed = 8f;
public float BackwardSpeed = 4f;
public float StrafeSpeed = 5f;
public float RotateSpeed = 110f;
CharacterController m_CharacterController;
Vector3 m_LastPosition;
Animator m_Animator;
PhotonView m_PhotonView;
PhotonTransformView m_TransformView;
float m_AnimatorSpeed;
Vector3 m_CurrentMovement;
float m_CurrentTurnSpeed;
Vector3 playerVelocity;
private bool groundedPlayer;
private float jumpHeight = 0.9f;
private float gravityValue = -20.81f;
void Start()
{
m_CharacterController = GetComponent<CharacterController>();
m_Animator = GetComponent<Animator>();
m_PhotonView = GetComponent<PhotonView>();
m_TransformView = GetComponent<PhotonTransformView>();
}
void Update()
{
if (m_PhotonView.isMine == true)
{
ResetSpeedValues();
UpdateRotateMovement();
UpdateForwardMovement();
UpdateBackwardMovement();
UpdateStrafeMovement();
MoveCharacterController();
UpdateJump();
ApplySynchronizedValues();
}
UpdateAnimation();
}
void UpdateAnimation()
{
Vector3 movementVector = transform.position - m_LastPosition;
float speed = Vector3.Dot(movementVector.normalized, transform.forward);
float direction = Vector3.Dot(movementVector.normalized, transform.right);
if (Mathf.Abs(speed) < 0.2f)
{
speed = 0f;
}
if (speed > 0.6f)
{
speed = 1f;
direction = 0f;
}
if (speed >= 0f)
{
if (Mathf.Abs(direction) > 0.7f)
{
speed = 1f;
}
}
m_AnimatorSpeed = Mathf.MoveTowards(m_AnimatorSpeed, speed, Time.deltaTime * 5f);
m_Animator.SetFloat("Speed", m_AnimatorSpeed);
m_Animator.SetFloat("Direction", direction);
m_LastPosition = transform.position;
}
void ResetSpeedValues()
{
m_CurrentMovement = Vector3.zero;
m_CurrentTurnSpeed = 0;
}
void ApplySynchronizedValues()
{
m_TransformView.SetSynchronizedValues(m_CurrentMovement, m_CurrentTurnSpeed);
}
void MoveCharacterController()
{
m_CharacterController.Move(m_CurrentMovement * Time.deltaTime);
}
void UpdateForwardMovement()
{
if (Input.GetKey(KeyCode.W) || Input.GetAxisRaw("Vertical") > 0.1f)
{
m_CurrentMovement = transform.forward * ForwardSpeed;
}
}
void UpdateBackwardMovement()
{
if (Input.GetKey(KeyCode.S) || Input.GetAxisRaw("Vertical") < -0.1f)
{
m_CurrentMovement = -transform.forward * BackwardSpeed;
}
}
void UpdateStrafeMovement()
{
if (Input.GetKey(KeyCode.Q) == true)
{
m_CurrentMovement = -transform.right * StrafeSpeed;
}
if (Input.GetKey(KeyCode.E) == true)
{
m_CurrentMovement = transform.right * StrafeSpeed;
}
}
void UpdateRotateMovement()
{
if (Input.GetKey(KeyCode.A) || Input.GetAxisRaw("Horizontal") < -0.1f)
{
m_CurrentTurnSpeed = -RotateSpeed;
transform.Rotate(0.0f, -RotateSpeed * Time.deltaTime, 0.0f);
}
if (Input.GetKey(KeyCode.D) || Input.GetAxisRaw("Horizontal") > 0.1f)
{
m_CurrentTurnSpeed = RotateSpeed;
transform.Rotate(0.0f, RotateSpeed * Time.deltaTime, 0.0f);
}
}
void UpdateJump()
{
groundedPlayer = m_CharacterController.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
m_Animator.SetTrigger("Jump");
print("Jumping Now");
}
playerVelocity.y += gravityValue * Time.deltaTime;
m_CharacterController.Move(playerVelocity * Time.deltaTime);
}
}
Best guess is that "m_PhotonView.isMine" is not returning true on the frames where you're missing input. It only checks jump input for that frame, so if the last frame you pressed it but jumping wasn't checked then that input is lost forever. First test this. Change the update code to this:
void Update()
{
if (Input.GetButtonDown("Jump")) { Debug.Log("Jump was pressed at {Time.time}"); }
if (m_PhotonView.isMine == true)
{
if (Input.GetButtonDown("Jump")) { Debug.Log("Attempting Jump at {Time.time}"); }
ResetSpeedValues();
UpdateRotateMovement();
UpdateForwardMovement();
UpdateBackwardMovement();
UpdateStrafeMovement();
MoveCharacterController();
UpdateJump();
ApplySynchronizedValues();
}
UpdateAnimation();
}
Then play the game and jump a bunch. The first debug log line should happen every time you click the spacebar no matter what. The second debug line would only happen if physics are calculated that frame. Both have times attached. Keep jumping until the jump doesn't work. If that jump only produces the first debug log and not the second, then I am correct and that is your issue.
If so, then it's an easy fix. Add a new bool variable called "jumpInput". Whenever you check if jump was pressed, instead check if "jumpInput" is true. Then, change update to this:
void Update()
{
if (Input.GetButtonDown("Jump")) { jumpInput = true; }
if (m_PhotonView.isMine == true)
{
ResetSpeedValues();
UpdateRotateMovement();
UpdateForwardMovement();
UpdateBackwardMovement();
UpdateStrafeMovement();
MoveCharacterController();
UpdateJump();
ApplySynchronizedValues();
jumpInput = false;
}
UpdateAnimation();
}
This way if you pressed jump, it's set to true... but it's only set to false after physics are done. So if you press jump on frame 20 and physics are somehow not calculated until frame 25, it'll still know that you pressed jump at some point and thus execute it. If you're using networking, you might want to also have another variable that's what frame jump was pressed. That way you can figure out how many frames it's been since input and compensate for missed time in the jump if necessary.

Unity 2D Jump script

So I was trying to implement double jumping in my game, which doesn't work. And now, somehow, not only can't my players double jump, they can't even jump either!
update: they can jump now, still can't double jump though.
This is my whole movement script:
using UnityEngine;
namespace Players
{
public class Actor : MonoBehaviour
{
//in order to control both players using 1 script.
public int playerIdx;
//Variables.
public float movementSpeed = 150f;
public float jumpForce = 250f;
//Ground stuff.
public LayerMask whatIsGround;
public bool grounded;
//boolean stuff.
private bool facingRight;
private bool moving;
//Needed to check if player is on the ground.
public Transform groundCheck;
//Limit player's movement speed.
public float maxMovementSpeed = 400f;
//Double jump stuff.
private bool doubleJumpReady;
//rb
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
doubleJumpReady = true;
rb = GetComponent<Rigidbody2D>();
facingRight = true;
}
// Update is called once per frame
void FixedUpdate()
{
SlowDown();
}
private void LateUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, whatIsGround);
if (grounded)
doubleJumpReady = true;
}
private void SlowDown()
{
if (moving) return;
//if player is not moving, slow them down.
if (rb.velocity.x > 0.2f)
rb.AddForce(movementSpeed * Time.deltaTime * -Vector2.right);
if (rb.velocity.x < -0.2f)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right);
}
public void Move(int dir)
{
//Flip the player.
Flip(dir);
//Moving the player.
moving = true;
float xVel = rb.velocity.x; //Get x velocity.
if ( dir > 0)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
else if (dir < 0)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
else if (dir == 0) { } //do nothing.
//Help player turn around faster.
if (xVel > 0.2f && dir < 0)
rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * -Vector2.right);
if (xVel < 0.2f && dir > 0)
rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * Vector2.right);
}
private void Flip(int dir)
{
if (facingRight && dir == -1 || !facingRight && dir == 1)
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
}
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
grounded = false;
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
}
}
}
}
I don't know if it is because of my jump script, or my player script:
void Update()
{
if (playerIdx == 1)
{
if (Input.GetKey(KeyCode.A))
Move(-1);
if (Input.GetKey(KeyCode.D))
Move(1);
if (Input.GetKey(KeyCode.W))
Jump();
}
if (playerIdx == 2)
{
if (Input.GetKey(KeyCode.LeftArrow))
Move(-1);
if (Input.GetKey(KeyCode.RightArrow))
Move(1);
if (Input.GetKey(KeyCode.UpArrow))
Jump();
}
}
So how can I fix this?
as far as i can see you never reset the
doubleJumpReady = false;
Variable. To fix this simply change the jump code to:
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
grounded = false;
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
}
}
Hope it works ;).
EDIT:
grounded is set by overlapping spheres. Therefore no need to set it here.
Use this code and press your jump btn 2 times and see if the Debug.Log message shows up. Also, your player ID (idx is not needed.) As far as i can see your script is attached two to different objects. Therefore their variables are not shared anyways.
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
Debug.Log("I am double jumping");
}
}
And the final problem is, you do not execute one of your jumps you execute both at once.
THis happens due to your execution.
Input.GetKey(KeyCode.UP)
instead use:
Input.GetKeyDown(KeyCode.Up);
GetKeyDown returns true when the button is pressed.
GetKey returns true WHILE the button is pressed.
Hope it works now ;)
I would implement it with a counter, you can set the number of jumps you want.
The code would be like this:
jumpCount = 0;
protected void Jump()
{
if(!grounded && jumpCount < 2)
{
jumpCount++;
rb.AddForce(Vector2.up * jumpForce);
}
if(grounded)
jumpCount = 0;
}
Going off the assumption that you can now perform the normal jump again after reading the comments. I think the reason you can't 'double jump' is that when you call the Jump() method, you don't just call it once, you call it twice, so what happens is the player jumps and then immediately double jumps and so you don't actually notice that the double jump has occurred. You could make it so that your doubleJumpReady boolean is only true after a set amount of time after you have jumped initially using some sort of co-routine or something I implemented for a sort of double jump mechanic once was that the user could press the jump button again to double jump only when the player had reached the maximum height of the initial jump or after.

Categories