Question by
mackanford7 · May 07, 2018 at 12:49 PM ·
c#unity 2dprogramming
Diagonal Wall Jump help
Hello! I am trying to make it so my character can jump diagonally off walls, but I am struggling hard. I was wondering if anyone could give me a hand regarding it or just general advice on my good in general. Please feel free to ask questions. Right now the Player Jumps on the wall, but does so vertically meaning they do not get pushed off the wall.
[Header("Player Movement Attributes")]
private float playerSpeed = 10f;
private float speedMultiplier = 1.0f;
[Header("Player Jump Attributes")]
[Range(5, 30)]
public float jumpVelocity = 10f;
public float lowJumpMultiplier = 2.5f;
public float fallMultiplier = 2;
private float jumpMultiplier = 1.0f;
private int extraJumps;
public int extraJumpsValue = 1;
[Header("Player Wall Attributes")]
[Range(5, 2000)]
public float wallPushForce = 10f;
// Input variables //
private string horizontal = "Horizontal";
private string jump = "Jump";
private Rigidbody2D rb;
private SpriteRenderer sr;
private void Awake()
{
if (Input.GetJoystickNames().Length != 0)
{
horizontal = "Horizontal_" + gameObject.name;
jump = "Jump_" + gameObject.name;
}
else
{
print("No controller detected!");
}
rb = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
}
protected override void RacerMove()
{
Leap();
Move();
}
private void Move()
{
float move = Input.GetAxis(horizontal);
rb.velocity = new Vector2(move * playerSpeed * speedMultiplier, rb.velocity.y);
if (move < 0)
{
sr.flipX = true;
}
else if (move > 0)
{
sr.flipX = false;
}
}
private void Leap()
{
// Reset Grounded //
if (isGrounded)
{
// On Ground
extraJumps = extraJumpsValue;
}
// Control Leap //
if (Input.GetButtonDown(jump) && isGrounded)
{
rb.velocity = Vector2.up * jumpVelocity * jumpMultiplier;
FindObjectOfType<AudioManager>().Play("RacerJump");
}
else if (Input.GetButtonDown(jump) && extraJumps > 0 && !isGrounded && !(isWallLeft || isWallRight))
{
// Air Jump
rb.velocity = Vector2.up * jumpVelocity * jumpMultiplier;
//rb.AddForce(Vector2.up * jumpVelocity);
FindObjectOfType<AudioManager>().Play("RacerJump");
extraJumps--;
}
else if (Input.GetButtonDown(jump) && (isWallLeft || isWallRight) && !isGrounded)
{
print("Saddness");
FindObjectOfType<AudioManager>().Play("RacerJump");
//rb.AddForce( new Vector2((isWallRight ? -1 : 1) * wallPushForce * Time.deltaTime, 1 * wallPushForce));
//rb.velocity = Vector2.up * jumpVelocity * jumpMultiplier;
//rb.velocity = new Vector2(wallPushForce * (isWallRight ? -1 : 1), wallPushForce);
}
// Leap Up & Downs //
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
else if (rb.velocity.y > 0 && !(isWallLeft || isWallLeft) && !Input.GetButton(jump))
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
}
}
Comment