Not able to make Wall Jump properly
up vote
0
down vote
favorite
I am trying to make my player jump from wall. I know I can do that by adding force in opposite direction. but my player is jumping weirdly and I tried to solve it by reading and watching tutorial but can't do it by myself. I think I need more professional and experienced mind to solve this.
I have 2 script one take care of wall jump and other script is responsible for movement and jump from ground and in this script i have applied if I pressed button longer player will jump higher.
This is the PlayerScript
public class PLayer : MonoBehaviour {
Animator animator;
float input;
public float moveSpeed = 10;
bool jumpButtonPressed = false;
bool jumpButtonHold = false;
float startingMoveSpeed;
[Tooltip("move Speed when player jump")]
public float jumpMoveSpeedDecrementor = 3;
public float jumpForce = 10;
Rigidbody2D rgbd;
float groundCheckRadius = 0.1f;
[HideInInspector]
public bool grounded,wallJump;
bool isJumping;
public bool facingRight;
bool isGrounded;
public Transform groundCheck;
public LayerMask ground;
public float gravityMultiplier = 2.5f;
float jumpTimeCounter;
public float jumpTime=0.35f;
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
rgbd = GetComponent<Rigidbody2D>();
startingMoveSpeed = moveSpeed;
wallJump = false;
}
private void Update()
{
if (!jumpButtonPressed)
{
jumpButtonPressed = Input.GetKeyDown(KeyCode.Z);
}
if (!jumpButtonHold)
{
jumpButtonHold = Input.GetKey(KeyCode.Z);
}
}
// Update is called once per frame
void FixedUpdate ()
{
input = Input.GetAxis ("Horizontal");
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
if (grounded|| wallJump)
{
animator.SetFloat("horizontalSpeed", Mathf.Abs(input));
moveSpeed = startingMoveSpeed;
}
else
{
moveSpeed = jumpMoveSpeedDecrementor;
}
if ((input > 0)&facingRight)
{
flipUsingScale();
}
if ((input < 0)&!facingRight)
{
flipUsingScale();
}
if (input != 0)
{
// transform.Translate(transform.right * input * moveSpeed * Time.deltaTime);
rgbd.velocity = new Vector2(input * moveSpeed, rgbd.velocity.y);
}
if (jumpButtonPressed && grounded )
{
isJumping = true;
animator.SetTrigger("jump");
rgbd.AddForce(Vector2.up * jumpForce*Time.deltaTime,ForceMode2D.Impulse);
jumpTimeCounter = jumpTime;
jumpButtonPressed = false;
}
if (jumpButtonHold && isJumping)
{
input = 0;
if (jumpTimeCounter > 0)
{
rgbd.AddForce(Vector2.up * jumpForce*Time.deltaTime);
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
jumpButtonHold = false;
}
}
if (Input.GetKeyUp(KeyCode.Z))
{
isJumping = false;
}
if (rgbd.velocity.y < 0)
{ Vector2 value = rgbd.velocity;
rgbd.velocity += Vector2.up * Physics2D.gravity.y *
(gravityMultiplier - 1) * Time.deltaTime;
}
}
#region flip The Player Direction
void flipUsingEuler(float input)
{
if (input > 0)
{
facingRight = true;
transform.eulerAngles = new Vector3(0, 0, 0);
}
if (input < 0)
{
facingRight = false;
transform.eulerAngles = new Vector3(0, 180, 0);
}
}
public void flipUsingScale()
{
facingRight = !facingRight;
float scale = -1;
Vector3 localScale = transform.localScale;
localScale.x *= scale;
transform.localScale = localScale;
}
#endregion
}
This is the second script which take care of the wall jump.I have added physics material to the wall with 0.02f friction.
public class WallJump : MonoBehaviour {
PLayer player;
public float rayDistance;
public int speed = 20;
public int speedY=300;
public LayerMask playerLayer;
// Use this for initialization
void Start () {
player = GetComponent<PLayer>();
}
// Update is called once per frame
void Update () {
Physics2D.queriesStartInColliders= false;
RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.right*transform.localScale.x, rayDistance);
if (Input.GetKeyDown(KeyCode.Z ) && !player.grounded && hit.collider.tag=="wall")
{
player.wallJump = true;
Debug.Log("adding force");
GetComponent<Rigidbody2D>().AddForce(new Vector2(speedY*hit.normal.x,speedY),ForceMode2D.Impulse);
// GetComponent<PLayer>().flipUsingScale();
}
else
{
player.wallJump = false;
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, transform.position+ Vector3.right * transform.localScale.x * rayDistance);
}
}
Thank you for reading and helping immature programmer like me.
c# unity3d 2d game-physics
add a comment |
up vote
0
down vote
favorite
I am trying to make my player jump from wall. I know I can do that by adding force in opposite direction. but my player is jumping weirdly and I tried to solve it by reading and watching tutorial but can't do it by myself. I think I need more professional and experienced mind to solve this.
I have 2 script one take care of wall jump and other script is responsible for movement and jump from ground and in this script i have applied if I pressed button longer player will jump higher.
This is the PlayerScript
public class PLayer : MonoBehaviour {
Animator animator;
float input;
public float moveSpeed = 10;
bool jumpButtonPressed = false;
bool jumpButtonHold = false;
float startingMoveSpeed;
[Tooltip("move Speed when player jump")]
public float jumpMoveSpeedDecrementor = 3;
public float jumpForce = 10;
Rigidbody2D rgbd;
float groundCheckRadius = 0.1f;
[HideInInspector]
public bool grounded,wallJump;
bool isJumping;
public bool facingRight;
bool isGrounded;
public Transform groundCheck;
public LayerMask ground;
public float gravityMultiplier = 2.5f;
float jumpTimeCounter;
public float jumpTime=0.35f;
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
rgbd = GetComponent<Rigidbody2D>();
startingMoveSpeed = moveSpeed;
wallJump = false;
}
private void Update()
{
if (!jumpButtonPressed)
{
jumpButtonPressed = Input.GetKeyDown(KeyCode.Z);
}
if (!jumpButtonHold)
{
jumpButtonHold = Input.GetKey(KeyCode.Z);
}
}
// Update is called once per frame
void FixedUpdate ()
{
input = Input.GetAxis ("Horizontal");
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
if (grounded|| wallJump)
{
animator.SetFloat("horizontalSpeed", Mathf.Abs(input));
moveSpeed = startingMoveSpeed;
}
else
{
moveSpeed = jumpMoveSpeedDecrementor;
}
if ((input > 0)&facingRight)
{
flipUsingScale();
}
if ((input < 0)&!facingRight)
{
flipUsingScale();
}
if (input != 0)
{
// transform.Translate(transform.right * input * moveSpeed * Time.deltaTime);
rgbd.velocity = new Vector2(input * moveSpeed, rgbd.velocity.y);
}
if (jumpButtonPressed && grounded )
{
isJumping = true;
animator.SetTrigger("jump");
rgbd.AddForce(Vector2.up * jumpForce*Time.deltaTime,ForceMode2D.Impulse);
jumpTimeCounter = jumpTime;
jumpButtonPressed = false;
}
if (jumpButtonHold && isJumping)
{
input = 0;
if (jumpTimeCounter > 0)
{
rgbd.AddForce(Vector2.up * jumpForce*Time.deltaTime);
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
jumpButtonHold = false;
}
}
if (Input.GetKeyUp(KeyCode.Z))
{
isJumping = false;
}
if (rgbd.velocity.y < 0)
{ Vector2 value = rgbd.velocity;
rgbd.velocity += Vector2.up * Physics2D.gravity.y *
(gravityMultiplier - 1) * Time.deltaTime;
}
}
#region flip The Player Direction
void flipUsingEuler(float input)
{
if (input > 0)
{
facingRight = true;
transform.eulerAngles = new Vector3(0, 0, 0);
}
if (input < 0)
{
facingRight = false;
transform.eulerAngles = new Vector3(0, 180, 0);
}
}
public void flipUsingScale()
{
facingRight = !facingRight;
float scale = -1;
Vector3 localScale = transform.localScale;
localScale.x *= scale;
transform.localScale = localScale;
}
#endregion
}
This is the second script which take care of the wall jump.I have added physics material to the wall with 0.02f friction.
public class WallJump : MonoBehaviour {
PLayer player;
public float rayDistance;
public int speed = 20;
public int speedY=300;
public LayerMask playerLayer;
// Use this for initialization
void Start () {
player = GetComponent<PLayer>();
}
// Update is called once per frame
void Update () {
Physics2D.queriesStartInColliders= false;
RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.right*transform.localScale.x, rayDistance);
if (Input.GetKeyDown(KeyCode.Z ) && !player.grounded && hit.collider.tag=="wall")
{
player.wallJump = true;
Debug.Log("adding force");
GetComponent<Rigidbody2D>().AddForce(new Vector2(speedY*hit.normal.x,speedY),ForceMode2D.Impulse);
// GetComponent<PLayer>().flipUsingScale();
}
else
{
player.wallJump = false;
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, transform.position+ Vector3.right * transform.localScale.x * rayDistance);
}
}
Thank you for reading and helping immature programmer like me.
c# unity3d 2d game-physics
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to make my player jump from wall. I know I can do that by adding force in opposite direction. but my player is jumping weirdly and I tried to solve it by reading and watching tutorial but can't do it by myself. I think I need more professional and experienced mind to solve this.
I have 2 script one take care of wall jump and other script is responsible for movement and jump from ground and in this script i have applied if I pressed button longer player will jump higher.
This is the PlayerScript
public class PLayer : MonoBehaviour {
Animator animator;
float input;
public float moveSpeed = 10;
bool jumpButtonPressed = false;
bool jumpButtonHold = false;
float startingMoveSpeed;
[Tooltip("move Speed when player jump")]
public float jumpMoveSpeedDecrementor = 3;
public float jumpForce = 10;
Rigidbody2D rgbd;
float groundCheckRadius = 0.1f;
[HideInInspector]
public bool grounded,wallJump;
bool isJumping;
public bool facingRight;
bool isGrounded;
public Transform groundCheck;
public LayerMask ground;
public float gravityMultiplier = 2.5f;
float jumpTimeCounter;
public float jumpTime=0.35f;
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
rgbd = GetComponent<Rigidbody2D>();
startingMoveSpeed = moveSpeed;
wallJump = false;
}
private void Update()
{
if (!jumpButtonPressed)
{
jumpButtonPressed = Input.GetKeyDown(KeyCode.Z);
}
if (!jumpButtonHold)
{
jumpButtonHold = Input.GetKey(KeyCode.Z);
}
}
// Update is called once per frame
void FixedUpdate ()
{
input = Input.GetAxis ("Horizontal");
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
if (grounded|| wallJump)
{
animator.SetFloat("horizontalSpeed", Mathf.Abs(input));
moveSpeed = startingMoveSpeed;
}
else
{
moveSpeed = jumpMoveSpeedDecrementor;
}
if ((input > 0)&facingRight)
{
flipUsingScale();
}
if ((input < 0)&!facingRight)
{
flipUsingScale();
}
if (input != 0)
{
// transform.Translate(transform.right * input * moveSpeed * Time.deltaTime);
rgbd.velocity = new Vector2(input * moveSpeed, rgbd.velocity.y);
}
if (jumpButtonPressed && grounded )
{
isJumping = true;
animator.SetTrigger("jump");
rgbd.AddForce(Vector2.up * jumpForce*Time.deltaTime,ForceMode2D.Impulse);
jumpTimeCounter = jumpTime;
jumpButtonPressed = false;
}
if (jumpButtonHold && isJumping)
{
input = 0;
if (jumpTimeCounter > 0)
{
rgbd.AddForce(Vector2.up * jumpForce*Time.deltaTime);
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
jumpButtonHold = false;
}
}
if (Input.GetKeyUp(KeyCode.Z))
{
isJumping = false;
}
if (rgbd.velocity.y < 0)
{ Vector2 value = rgbd.velocity;
rgbd.velocity += Vector2.up * Physics2D.gravity.y *
(gravityMultiplier - 1) * Time.deltaTime;
}
}
#region flip The Player Direction
void flipUsingEuler(float input)
{
if (input > 0)
{
facingRight = true;
transform.eulerAngles = new Vector3(0, 0, 0);
}
if (input < 0)
{
facingRight = false;
transform.eulerAngles = new Vector3(0, 180, 0);
}
}
public void flipUsingScale()
{
facingRight = !facingRight;
float scale = -1;
Vector3 localScale = transform.localScale;
localScale.x *= scale;
transform.localScale = localScale;
}
#endregion
}
This is the second script which take care of the wall jump.I have added physics material to the wall with 0.02f friction.
public class WallJump : MonoBehaviour {
PLayer player;
public float rayDistance;
public int speed = 20;
public int speedY=300;
public LayerMask playerLayer;
// Use this for initialization
void Start () {
player = GetComponent<PLayer>();
}
// Update is called once per frame
void Update () {
Physics2D.queriesStartInColliders= false;
RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.right*transform.localScale.x, rayDistance);
if (Input.GetKeyDown(KeyCode.Z ) && !player.grounded && hit.collider.tag=="wall")
{
player.wallJump = true;
Debug.Log("adding force");
GetComponent<Rigidbody2D>().AddForce(new Vector2(speedY*hit.normal.x,speedY),ForceMode2D.Impulse);
// GetComponent<PLayer>().flipUsingScale();
}
else
{
player.wallJump = false;
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, transform.position+ Vector3.right * transform.localScale.x * rayDistance);
}
}
Thank you for reading and helping immature programmer like me.
c# unity3d 2d game-physics
I am trying to make my player jump from wall. I know I can do that by adding force in opposite direction. but my player is jumping weirdly and I tried to solve it by reading and watching tutorial but can't do it by myself. I think I need more professional and experienced mind to solve this.
I have 2 script one take care of wall jump and other script is responsible for movement and jump from ground and in this script i have applied if I pressed button longer player will jump higher.
This is the PlayerScript
public class PLayer : MonoBehaviour {
Animator animator;
float input;
public float moveSpeed = 10;
bool jumpButtonPressed = false;
bool jumpButtonHold = false;
float startingMoveSpeed;
[Tooltip("move Speed when player jump")]
public float jumpMoveSpeedDecrementor = 3;
public float jumpForce = 10;
Rigidbody2D rgbd;
float groundCheckRadius = 0.1f;
[HideInInspector]
public bool grounded,wallJump;
bool isJumping;
public bool facingRight;
bool isGrounded;
public Transform groundCheck;
public LayerMask ground;
public float gravityMultiplier = 2.5f;
float jumpTimeCounter;
public float jumpTime=0.35f;
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
rgbd = GetComponent<Rigidbody2D>();
startingMoveSpeed = moveSpeed;
wallJump = false;
}
private void Update()
{
if (!jumpButtonPressed)
{
jumpButtonPressed = Input.GetKeyDown(KeyCode.Z);
}
if (!jumpButtonHold)
{
jumpButtonHold = Input.GetKey(KeyCode.Z);
}
}
// Update is called once per frame
void FixedUpdate ()
{
input = Input.GetAxis ("Horizontal");
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
if (grounded|| wallJump)
{
animator.SetFloat("horizontalSpeed", Mathf.Abs(input));
moveSpeed = startingMoveSpeed;
}
else
{
moveSpeed = jumpMoveSpeedDecrementor;
}
if ((input > 0)&facingRight)
{
flipUsingScale();
}
if ((input < 0)&!facingRight)
{
flipUsingScale();
}
if (input != 0)
{
// transform.Translate(transform.right * input * moveSpeed * Time.deltaTime);
rgbd.velocity = new Vector2(input * moveSpeed, rgbd.velocity.y);
}
if (jumpButtonPressed && grounded )
{
isJumping = true;
animator.SetTrigger("jump");
rgbd.AddForce(Vector2.up * jumpForce*Time.deltaTime,ForceMode2D.Impulse);
jumpTimeCounter = jumpTime;
jumpButtonPressed = false;
}
if (jumpButtonHold && isJumping)
{
input = 0;
if (jumpTimeCounter > 0)
{
rgbd.AddForce(Vector2.up * jumpForce*Time.deltaTime);
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
jumpButtonHold = false;
}
}
if (Input.GetKeyUp(KeyCode.Z))
{
isJumping = false;
}
if (rgbd.velocity.y < 0)
{ Vector2 value = rgbd.velocity;
rgbd.velocity += Vector2.up * Physics2D.gravity.y *
(gravityMultiplier - 1) * Time.deltaTime;
}
}
#region flip The Player Direction
void flipUsingEuler(float input)
{
if (input > 0)
{
facingRight = true;
transform.eulerAngles = new Vector3(0, 0, 0);
}
if (input < 0)
{
facingRight = false;
transform.eulerAngles = new Vector3(0, 180, 0);
}
}
public void flipUsingScale()
{
facingRight = !facingRight;
float scale = -1;
Vector3 localScale = transform.localScale;
localScale.x *= scale;
transform.localScale = localScale;
}
#endregion
}
This is the second script which take care of the wall jump.I have added physics material to the wall with 0.02f friction.
public class WallJump : MonoBehaviour {
PLayer player;
public float rayDistance;
public int speed = 20;
public int speedY=300;
public LayerMask playerLayer;
// Use this for initialization
void Start () {
player = GetComponent<PLayer>();
}
// Update is called once per frame
void Update () {
Physics2D.queriesStartInColliders= false;
RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.right*transform.localScale.x, rayDistance);
if (Input.GetKeyDown(KeyCode.Z ) && !player.grounded && hit.collider.tag=="wall")
{
player.wallJump = true;
Debug.Log("adding force");
GetComponent<Rigidbody2D>().AddForce(new Vector2(speedY*hit.normal.x,speedY),ForceMode2D.Impulse);
// GetComponent<PLayer>().flipUsingScale();
}
else
{
player.wallJump = false;
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, transform.position+ Vector3.right * transform.localScale.x * rayDistance);
}
}
Thank you for reading and helping immature programmer like me.
c# unity3d 2d game-physics
c# unity3d 2d game-physics
edited 3 hours ago
Programmer
72.8k1075135
72.8k1075135
asked 12 hours ago
user5234003
327
327
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203293%2fnot-able-to-make-wall-jump-properly%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password