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.



Jump error










share|improve this question




























    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.



    Jump error










    share|improve this question


























      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.



      Jump error










      share|improve this question















      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.



      Jump error







      c# unity3d 2d game-physics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 hours ago









      Programmer

      72.8k1075135




      72.8k1075135










      asked 12 hours ago









      user5234003

      327




      327





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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




















































































          Popular posts from this blog

          Schultheiß

          Liste der Kulturdenkmale in Wilsdruff

          Android Play Services Check