unity 2d wall jump
plz help guys i dont know why rigidbody2d.AddForce dont work with rigidbody2d.velocity and this the script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerC : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
Animator anim;
bool grounded = false;
bool touchingWall;
public float jumpPushForce = 10f;
public Transform groundCheck;
public Transform wallCheck;
float groundRadius = 0.2f;
float wallRadius = 0.2f;
public LayerMask whatIsGround;
public LayerMask whatIsWall;
public float jumpForce = 700f;
void Start ()
{
anim = GetComponent<Animator> ();
}
void FixedUpdate ()
{
var rb = GetComponent<Rigidbody2D>();
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
touchingWall = Physics2D.OverlapCircle(wallCheck.position, wallRadius, whatIsWall);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", rb.velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
rb.velocity = new Vector2 (move * maxSpeed, rb.velocity.y);
//flip the player
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
if (touchingWall)
{
grounded = false;
}
}
void Update()
{
var rb = GetComponent<Rigidbody2D>();
if (grounded && Input.GetKeyDown (KeyCode.Space))
{
anim.SetBool ("Ground", false);
rb.AddForce (new Vector2(0, jumpForce));
}
if (touchingWall && Input.GetButtonDown ("Jump"))
{
WallJump ();
}
}
void Flip ()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void WallJump ()
{
var rb = GetComponent<Rigidbody2D>();
if (!facingRight) {
Flip ();
rb.AddForce (new Vector2 (jumpPushForce,jumpForce));
}else if (facingRight) {
rb.AddForce (new Vector2 (-jumpPushForce,jumpForce));
Flip ();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
2D Spine Animation not playing 0 Answers
2d animation mixing 1 Answer
how long a bottom being pressed [solved] 3 Answers
Animation stuck in jump whenever I play. 0 Answers
Later grab doesn't jump 0 Answers