Question by
Snase · Apr 10, 2016 at 09:39 AM ·
c#player2d-physics
Hi, I would like my player to push away from a wall sort of like ninja game. After toying around a bit I'm stuck. Anyone got any tips?
Ok so I'm new to unity and I would like my player to gain some kind of force away from the wall when he's stuck on the wall and the spacebar is pushed. The code that isn't working as I would like to is at the first if statement. Here's the code:
public float playerMovement;
public float jumpHeight;
public float runSpeed;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJump;
private bool wallTapL;
public Transform wallTapCheckL;
public float wallTapCheckRadiusL;
public LayerMask wallTapCheckLayerCheckL;
private bool wallTapR;
public Transform wallTapCheckR;
public float wallTapCheckRadiusR;
public LayerMask wallTapCheckLayerCheckR;
public float wallPushR;
void Start () {
}
void Update () {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
wallTapL = Physics2D.OverlapCircle(wallTapCheckL.position, wallTapCheckRadiusL, wallTapCheckLayerCheckL);
wallTapR = Physics2D.OverlapCircle(wallTapCheckR.position, wallTapCheckRadiusR, wallTapCheckLayerCheckR);
if (Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.D) && wallTapL && !grounded)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(wallPushR * 1, GetComponent<Rigidbody2D>().velocity.y);
}
if (wallTapR && !grounded)
{
doubleJump = false;
}
if (wallTapL && !grounded)
{
doubleJump = false;
}
if (grounded)
{
doubleJump = false;
}
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
}
if (Input.GetKeyDown(KeyCode.Space) && !doubleJump && !grounded)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
doubleJump = true;
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-playerMovement, GetComponent<Rigidbody2D>().velocity.y);
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(playerMovement, GetComponent<Rigidbody2D>().velocity.y);
}
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.LeftShift))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-(playerMovement + jumpHeight), GetComponent<Rigidbody2D>().velocity.y);
}
if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.LeftShift))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(playerMovement + jumpHeight, GetComponent<Rigidbody2D>().velocity.y);
}
}
}
Comment
Probably need a few elses in there...
"isn't working as I would like" doesnt explain much.
Your answer
![](https://koobas.hobune.stream/wayback/20220612074533im_/https://answers.unity.com/themes/thub/images/avi.jpg)