Player movement problem using rays in Unity2d












0















I'm creating a 2D mario like platformer in Unity and I have ran in to a problem with player movement script. The thing is that the character is never trully on the ground, in the inspector it's y velocity allways changes from 0 to 0.3 and then back to 0 and so for an endless loop.



It might have something to do with this line




position.y = hitRay.collider.bounds.center.y + hitRay.collider.bounds.size.y / 2 + 0.0801f;




if I put it at 0.08f, (my players y size is 0.16f) my player just falls through the ground.



If anybody notices the mistake and is kind enough to help, I would be very grateful.



void Fall()
{
velocity.y = 0;

playerState = PlayerState.jumping;

grounded = false;

bounce = false;
}

void UpdatePlayerPosition()
{
Vector3 position = transform.localPosition;
Vector3 scale = transform.localScale;

if (walk)
{
if (walk_left)
{
position.x -= velocity.x * Time.deltaTime;

scale.x = -1;
}

if (walk_right)
{
position.x += velocity.x * Time.deltaTime;

scale.x = 1;
}

position = CheckWallRays(position, scale.x);
}

if (jump && playerState != PlayerState.jumping)
{
playerState = PlayerState.jumping;

velocity = new Vector2(velocity.x, jumpVelocity);
}

if (playerState == PlayerState.jumping)
{
position.y += velocity.y * Time.deltaTime;
velocity.y -= gravity * Time.deltaTime;
}

if (bounce && playerState != PlayerState.bouncing)
{
playerState = PlayerState.bouncing;

velocity = new Vector2(velocity.x, bounceVelocity);
}

if (playerState == PlayerState.bouncing)
{
position.y += velocity.y * Time.deltaTime;

velocity.y -= gravity * Time.deltaTime;
}

if (velocity.y <= 0)
{
position = CheckGroundRays(position);
}

if (velocity.y >= 0)
{
position = CheckCeilingRays(position);
}

transform.localPosition = position;
transform.localScale = scale;
}

Vector3 CheckGroundRays(Vector3 position)
{
Vector2 left = new Vector2(position.x - 0.065f, position.y - 0.08f);
Vector2 middle = new Vector2(position.x, position.y - 0.08f);
Vector2 right = new Vector2(position.x + 0.065f, position.y - 0.08f);

Debug.DrawRay(left, Vector2.down, Color.green);
Debug.DrawRay(middle, Vector2.down);
Debug.DrawRay(right, Vector2.down);

RaycastHit2D groundLeft = Physics2D.Raycast(left, Vector2.down, velocity.y * Time.deltaTime, floorMask);
RaycastHit2D groundMiddle = Physics2D.Raycast(middle, Vector2.down, velocity.y * Time.deltaTime, floorMask);
RaycastHit2D groundRight = Physics2D.Raycast(right, Vector2.down, velocity.y * Time.deltaTime, floorMask);

if (groundLeft.collider != null || groundMiddle.collider != null || groundRight.collider != null)
{
RaycastHit2D hitRay = groundLeft;

if (groundLeft)
{
hitRay = groundLeft;
}
else if (groundMiddle)
{
hitRay = groundMiddle;
}
else if (groundRight)
{
hitRay = groundRight;
}

if (hitRay.collider.tag == "Enemy")
{
hitRay.collider.GetComponent<EnemyAI>().Crush();

this.GetComponent<PlayerScore>().score += 100;

bounce = true;
}

playerState = PlayerState.idle;

grounded = true;

velocity.y = 0;

position.y = hitRay.collider.bounds.center.y + hitRay.collider.bounds.size.y / 2 + 0.0801f;
}
else if (playerState != PlayerState.jumping)
{
Fall();
}

return position;
}









share|improve this question





























    0















    I'm creating a 2D mario like platformer in Unity and I have ran in to a problem with player movement script. The thing is that the character is never trully on the ground, in the inspector it's y velocity allways changes from 0 to 0.3 and then back to 0 and so for an endless loop.



    It might have something to do with this line




    position.y = hitRay.collider.bounds.center.y + hitRay.collider.bounds.size.y / 2 + 0.0801f;




    if I put it at 0.08f, (my players y size is 0.16f) my player just falls through the ground.



    If anybody notices the mistake and is kind enough to help, I would be very grateful.



    void Fall()
    {
    velocity.y = 0;

    playerState = PlayerState.jumping;

    grounded = false;

    bounce = false;
    }

    void UpdatePlayerPosition()
    {
    Vector3 position = transform.localPosition;
    Vector3 scale = transform.localScale;

    if (walk)
    {
    if (walk_left)
    {
    position.x -= velocity.x * Time.deltaTime;

    scale.x = -1;
    }

    if (walk_right)
    {
    position.x += velocity.x * Time.deltaTime;

    scale.x = 1;
    }

    position = CheckWallRays(position, scale.x);
    }

    if (jump && playerState != PlayerState.jumping)
    {
    playerState = PlayerState.jumping;

    velocity = new Vector2(velocity.x, jumpVelocity);
    }

    if (playerState == PlayerState.jumping)
    {
    position.y += velocity.y * Time.deltaTime;
    velocity.y -= gravity * Time.deltaTime;
    }

    if (bounce && playerState != PlayerState.bouncing)
    {
    playerState = PlayerState.bouncing;

    velocity = new Vector2(velocity.x, bounceVelocity);
    }

    if (playerState == PlayerState.bouncing)
    {
    position.y += velocity.y * Time.deltaTime;

    velocity.y -= gravity * Time.deltaTime;
    }

    if (velocity.y <= 0)
    {
    position = CheckGroundRays(position);
    }

    if (velocity.y >= 0)
    {
    position = CheckCeilingRays(position);
    }

    transform.localPosition = position;
    transform.localScale = scale;
    }

    Vector3 CheckGroundRays(Vector3 position)
    {
    Vector2 left = new Vector2(position.x - 0.065f, position.y - 0.08f);
    Vector2 middle = new Vector2(position.x, position.y - 0.08f);
    Vector2 right = new Vector2(position.x + 0.065f, position.y - 0.08f);

    Debug.DrawRay(left, Vector2.down, Color.green);
    Debug.DrawRay(middle, Vector2.down);
    Debug.DrawRay(right, Vector2.down);

    RaycastHit2D groundLeft = Physics2D.Raycast(left, Vector2.down, velocity.y * Time.deltaTime, floorMask);
    RaycastHit2D groundMiddle = Physics2D.Raycast(middle, Vector2.down, velocity.y * Time.deltaTime, floorMask);
    RaycastHit2D groundRight = Physics2D.Raycast(right, Vector2.down, velocity.y * Time.deltaTime, floorMask);

    if (groundLeft.collider != null || groundMiddle.collider != null || groundRight.collider != null)
    {
    RaycastHit2D hitRay = groundLeft;

    if (groundLeft)
    {
    hitRay = groundLeft;
    }
    else if (groundMiddle)
    {
    hitRay = groundMiddle;
    }
    else if (groundRight)
    {
    hitRay = groundRight;
    }

    if (hitRay.collider.tag == "Enemy")
    {
    hitRay.collider.GetComponent<EnemyAI>().Crush();

    this.GetComponent<PlayerScore>().score += 100;

    bounce = true;
    }

    playerState = PlayerState.idle;

    grounded = true;

    velocity.y = 0;

    position.y = hitRay.collider.bounds.center.y + hitRay.collider.bounds.size.y / 2 + 0.0801f;
    }
    else if (playerState != PlayerState.jumping)
    {
    Fall();
    }

    return position;
    }









    share|improve this question



























      0












      0








      0








      I'm creating a 2D mario like platformer in Unity and I have ran in to a problem with player movement script. The thing is that the character is never trully on the ground, in the inspector it's y velocity allways changes from 0 to 0.3 and then back to 0 and so for an endless loop.



      It might have something to do with this line




      position.y = hitRay.collider.bounds.center.y + hitRay.collider.bounds.size.y / 2 + 0.0801f;




      if I put it at 0.08f, (my players y size is 0.16f) my player just falls through the ground.



      If anybody notices the mistake and is kind enough to help, I would be very grateful.



      void Fall()
      {
      velocity.y = 0;

      playerState = PlayerState.jumping;

      grounded = false;

      bounce = false;
      }

      void UpdatePlayerPosition()
      {
      Vector3 position = transform.localPosition;
      Vector3 scale = transform.localScale;

      if (walk)
      {
      if (walk_left)
      {
      position.x -= velocity.x * Time.deltaTime;

      scale.x = -1;
      }

      if (walk_right)
      {
      position.x += velocity.x * Time.deltaTime;

      scale.x = 1;
      }

      position = CheckWallRays(position, scale.x);
      }

      if (jump && playerState != PlayerState.jumping)
      {
      playerState = PlayerState.jumping;

      velocity = new Vector2(velocity.x, jumpVelocity);
      }

      if (playerState == PlayerState.jumping)
      {
      position.y += velocity.y * Time.deltaTime;
      velocity.y -= gravity * Time.deltaTime;
      }

      if (bounce && playerState != PlayerState.bouncing)
      {
      playerState = PlayerState.bouncing;

      velocity = new Vector2(velocity.x, bounceVelocity);
      }

      if (playerState == PlayerState.bouncing)
      {
      position.y += velocity.y * Time.deltaTime;

      velocity.y -= gravity * Time.deltaTime;
      }

      if (velocity.y <= 0)
      {
      position = CheckGroundRays(position);
      }

      if (velocity.y >= 0)
      {
      position = CheckCeilingRays(position);
      }

      transform.localPosition = position;
      transform.localScale = scale;
      }

      Vector3 CheckGroundRays(Vector3 position)
      {
      Vector2 left = new Vector2(position.x - 0.065f, position.y - 0.08f);
      Vector2 middle = new Vector2(position.x, position.y - 0.08f);
      Vector2 right = new Vector2(position.x + 0.065f, position.y - 0.08f);

      Debug.DrawRay(left, Vector2.down, Color.green);
      Debug.DrawRay(middle, Vector2.down);
      Debug.DrawRay(right, Vector2.down);

      RaycastHit2D groundLeft = Physics2D.Raycast(left, Vector2.down, velocity.y * Time.deltaTime, floorMask);
      RaycastHit2D groundMiddle = Physics2D.Raycast(middle, Vector2.down, velocity.y * Time.deltaTime, floorMask);
      RaycastHit2D groundRight = Physics2D.Raycast(right, Vector2.down, velocity.y * Time.deltaTime, floorMask);

      if (groundLeft.collider != null || groundMiddle.collider != null || groundRight.collider != null)
      {
      RaycastHit2D hitRay = groundLeft;

      if (groundLeft)
      {
      hitRay = groundLeft;
      }
      else if (groundMiddle)
      {
      hitRay = groundMiddle;
      }
      else if (groundRight)
      {
      hitRay = groundRight;
      }

      if (hitRay.collider.tag == "Enemy")
      {
      hitRay.collider.GetComponent<EnemyAI>().Crush();

      this.GetComponent<PlayerScore>().score += 100;

      bounce = true;
      }

      playerState = PlayerState.idle;

      grounded = true;

      velocity.y = 0;

      position.y = hitRay.collider.bounds.center.y + hitRay.collider.bounds.size.y / 2 + 0.0801f;
      }
      else if (playerState != PlayerState.jumping)
      {
      Fall();
      }

      return position;
      }









      share|improve this question
















      I'm creating a 2D mario like platformer in Unity and I have ran in to a problem with player movement script. The thing is that the character is never trully on the ground, in the inspector it's y velocity allways changes from 0 to 0.3 and then back to 0 and so for an endless loop.



      It might have something to do with this line




      position.y = hitRay.collider.bounds.center.y + hitRay.collider.bounds.size.y / 2 + 0.0801f;




      if I put it at 0.08f, (my players y size is 0.16f) my player just falls through the ground.



      If anybody notices the mistake and is kind enough to help, I would be very grateful.



      void Fall()
      {
      velocity.y = 0;

      playerState = PlayerState.jumping;

      grounded = false;

      bounce = false;
      }

      void UpdatePlayerPosition()
      {
      Vector3 position = transform.localPosition;
      Vector3 scale = transform.localScale;

      if (walk)
      {
      if (walk_left)
      {
      position.x -= velocity.x * Time.deltaTime;

      scale.x = -1;
      }

      if (walk_right)
      {
      position.x += velocity.x * Time.deltaTime;

      scale.x = 1;
      }

      position = CheckWallRays(position, scale.x);
      }

      if (jump && playerState != PlayerState.jumping)
      {
      playerState = PlayerState.jumping;

      velocity = new Vector2(velocity.x, jumpVelocity);
      }

      if (playerState == PlayerState.jumping)
      {
      position.y += velocity.y * Time.deltaTime;
      velocity.y -= gravity * Time.deltaTime;
      }

      if (bounce && playerState != PlayerState.bouncing)
      {
      playerState = PlayerState.bouncing;

      velocity = new Vector2(velocity.x, bounceVelocity);
      }

      if (playerState == PlayerState.bouncing)
      {
      position.y += velocity.y * Time.deltaTime;

      velocity.y -= gravity * Time.deltaTime;
      }

      if (velocity.y <= 0)
      {
      position = CheckGroundRays(position);
      }

      if (velocity.y >= 0)
      {
      position = CheckCeilingRays(position);
      }

      transform.localPosition = position;
      transform.localScale = scale;
      }

      Vector3 CheckGroundRays(Vector3 position)
      {
      Vector2 left = new Vector2(position.x - 0.065f, position.y - 0.08f);
      Vector2 middle = new Vector2(position.x, position.y - 0.08f);
      Vector2 right = new Vector2(position.x + 0.065f, position.y - 0.08f);

      Debug.DrawRay(left, Vector2.down, Color.green);
      Debug.DrawRay(middle, Vector2.down);
      Debug.DrawRay(right, Vector2.down);

      RaycastHit2D groundLeft = Physics2D.Raycast(left, Vector2.down, velocity.y * Time.deltaTime, floorMask);
      RaycastHit2D groundMiddle = Physics2D.Raycast(middle, Vector2.down, velocity.y * Time.deltaTime, floorMask);
      RaycastHit2D groundRight = Physics2D.Raycast(right, Vector2.down, velocity.y * Time.deltaTime, floorMask);

      if (groundLeft.collider != null || groundMiddle.collider != null || groundRight.collider != null)
      {
      RaycastHit2D hitRay = groundLeft;

      if (groundLeft)
      {
      hitRay = groundLeft;
      }
      else if (groundMiddle)
      {
      hitRay = groundMiddle;
      }
      else if (groundRight)
      {
      hitRay = groundRight;
      }

      if (hitRay.collider.tag == "Enemy")
      {
      hitRay.collider.GetComponent<EnemyAI>().Crush();

      this.GetComponent<PlayerScore>().score += 100;

      bounce = true;
      }

      playerState = PlayerState.idle;

      grounded = true;

      velocity.y = 0;

      position.y = hitRay.collider.bounds.center.y + hitRay.collider.bounds.size.y / 2 + 0.0801f;
      }
      else if (playerState != PlayerState.jumping)
      {
      Fall();
      }

      return position;
      }






      c# unity3d






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 13:57









      MX D

      1,98632337




      1,98632337










      asked Nov 13 '18 at 11:04









      MecholazMecholaz

      13




      13
























          0






          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',
          autoActivateHeartbeat: false,
          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%2f53279611%2fplayer-movement-problem-using-rays-in-unity2d%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279611%2fplayer-movement-problem-using-rays-in-unity2d%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ