Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by cordial_studios · Jul 04, 2021 at 01:21 AM · 2d gameplatformer

for some reason the isGrounded is always checked off for me

heres the code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NewBehaviourScript : MonoBehaviour
 {
     
     Rigidbody2D rb;
     Animator animator;
     [SerializeField] Transform groundCheckCollider;
     [SerializeField] LayerMask groundLayer;
 
     [SerializeField] const float groundCheckRadius = 0.2f;
     [SerializeField] public float speed;
     [SerializeField] public float jumpPower;
     float horizontalvalue;
     bool isRunning;
     bool facingRight = true;
     float runSpeedModifier = 2f;
     [SerializeField] bool isgrounded;
     [SerializeField] bool jump;
 
     void Awake()
     {
         rb = GetComponent<Rigidbody2D>();
         animator = GetComponent<Animator>();
     }
 
     void Update()
     {
         horizontalvalue = Input.GetAxisRaw("Horizontal");
         if(Input.GetKeyDown(KeyCode.LeftShift))
         {
             isRunning = true;
         }
         if (Input.GetKeyUp(KeyCode.LeftShift))
         {
             isRunning = false;
         }
         if (Input.GetKeyDown(KeyCode.Space))
         {
             jump = true;
         }
         else if (Input.GetKeyUp(KeyCode.Space))
         {
             jump = false;
         }
     }
     void FixedUpdate()
     {
         GroundCheck();
         Move(horizontalvalue, jump);
     }
     void GroundCheck()
     {
         isgrounded = false;
         Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer);
         if (colliders.Length > 0)
             isgrounded = true;
     }
     void Move(float dir,bool jumpFlag)
     {
         if(isgrounded && jumpFlag)
         {
             isgrounded = false;
             jumpFlag = false;
             rb.AddForce(new Vector2(0f,jumpPower));
         }
         #region move & run
         float xVal = dir * speed * 100 * Time.fixedDeltaTime;
         if (isRunning)
         {
             xVal *= runSpeedModifier;
         }
         Vector2 targetVelocity = new Vector2(xVal, rb.velocity.y);
         rb.velocity = targetVelocity;
 
         if (facingRight && dir < 0)
         {
             transform.localScale = new Vector3(-5, 5, 1);
             facingRight = false;
         }
         else if (!facingRight && dir > 0)
         {
             transform.localScale = new Vector3(5, 5, 1);
             facingRight = true;
         }
         animator.SetFloat("Xvelocity", Mathf.Abs(rb.velocity.x));
         #endregion
         
     }
 
 }
 
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anonymous620 · Jul 06, 2021 at 01:47 PM 0
Share

Could you please format the code so it's easier to read

avatar image Anonymous620 · Jul 08, 2021 at 01:09 PM 0
Share

Thanks for that, I will have a look when I can

avatar image Anonymous620 · Jul 09, 2021 at 02:03 AM 0
Share

@cordial_studios What type of game are you making? can you use raycast instead, this might be a better way of checking if you are grounded.

avatar image cordial_studios Anonymous620 · Jul 10, 2021 at 01:12 AM 0
Share

a platformer

avatar image cordial_studios · Jul 10, 2021 at 01:13 AM 0
Share

(i updated the code because i think i chnged it around a little and it didn't work

avatar image Anonymous620 cordial_studios · Jul 10, 2021 at 01:14 PM 0
Share

Try using raycast to detect the ground

avatar image cordial_studios Anonymous620 · Jul 12, 2021 at 12:11 AM 0
Share

what's that

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by In0sc0p3dJFK · Jul 09, 2021 at 05:09 AM

Look at line 49:


  void FixedUpdate()
  {

      GroundCheck();
      Move(horizontalvalue, jump);

  }
  void GroundCheck()
  {
      isgrounded = false;
  }



Your ground check function is always being called every frame because it's being called inside of FixedUpdate();. And inside of your ground check, you are always setting your isGrounded bool to false. Thus, your isGrounded bool is going to be false indefinitely. Try taking it out or putting it somewhere else.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cordial_studios · Jul 10, 2021 at 01:22 AM 0
Share

where should i move it to because none of the places im moving it to are aworking

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

207 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player passenger moving when being pushed by two platforms 0 Answers

Problems with player jumping (Welcome to Unity Answers The best place to ask and answer questions ) 0 Answers

Player gets stuck on moving platform 0 Answers

how to make a plataforme game with 3d depth 1 Answer

platformEffector2D pushing character upward,Platform Effector 2D pushing character if Keypressed 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges