• 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 /
avatar image
0
Question by Wab17 · Jun 18, 2017 at 01:02 AM · positionraycasthit2dgame development

Raycasting issue?

public class PlayerControllerCW : MonoBehaviour { private Rigidbody2D _player; public float Acceleration; public float maxVelocity; public float jumpForce; private bool grounded;

 // Use this for initialization
 void Start () {
     _player = GetComponent<Rigidbody2D>();
     
 }
 
 // Update is called once per frame
 void Update () {
     CheckIfGrounded();
     if (Input.GetKeyDown(KeyCode.W) && grounded) {
         _player.AddForce(new Vector2(0f, jumpForce));
     }
 }
 void CheckIfGrounded() {
     RaycastHit2D[] hits;
     //We raycast down 1 pixel from this position to check for a collider
     Vector2 positionToCheck = _player.position;
     hits = Physics2D.RaycastAll(positionToCheck, new Vector2(0,-1),0.01f);
     //if a collider was hit, we are grounded
     if (hits.Length > 0) {
         print("we are grounded!");
         grounded = true;
     }
     else {
         grounded = false;
     }
 }

I'm trying to program my own player controller but I'm having a big problem limiting jumping to only when grounded. It seems the raycast doesn't work as it constantly says "we are grounded" even though my sprite is literally flying through the air. I tried replacing positionToCheck to new Vector (10,10) and oddly enough the object was unable to move and nothing was being printed. Im using a polygon collider for the sprite and a box collider for the ground.

Thanks in advance

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by alph1 · Jun 18, 2017 at 04:31 AM

You Should fallow the steps below

  • Create a layer named "groundLayer"

  • Create a layerMask in your script and select only "groundLayer" in layer list on inspector

  • use only one raycastHit instead of array of them therefor first collder will be the closest point therefor its the closest landing ground

  • put your layerMask variable to your Raycast such as Physics2D.Raycast(castingPos,direction(In your case Vector2.down),maxdistance,layerMask);

    Wab17

Comment
Add comment · Show 2 · 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 Wab17 · Jun 18, 2017 at 10:35 AM 0
Share

That has done absolutely nothing, it still constantly registers me as grounded no matter where I am, it's something to do with the transform position but I dont know what the issue is.

EDIT: I think I know what the issue is, it is finding its own collider with the raycast, any idea how to move the raycast projection position to underneath the collider?

avatar image alph1 Wab17 · Jun 18, 2017 at 03:16 PM 0
Share

if you want to do that then you should create a empty gameObject and set position of the game Object the character's feet or where you want to start raycasting position and then make this object child of your character object and create a variable fieldl for your child gameObject get its position and place it to RaycastStart point.If you think this is too fake and complex way to do it then you could define a public Vector2 variable and every frame you can update your variable as variable+=new Vector2(this.transform.position.x,this.transform.position.y); and to detect the right place you can use Debug.DrawLine(variable,variable+Vector2.down*5); therefor you can monitor your position and place it where you want after this you can delete the debug.drawLine function and place the variable into your Raycast function.By the way avoid using multiple RaycastHit2D try to achieve with using layer$$anonymous$$ask and 1 raycastHit2d

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

66 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

Related Questions

Using Player Rotation Quaternion to Make a Directional Vector 1 Answer

Camera rotation around player while following. 6 Answers

How to spawn object but with child as center of position 0 Answers

Vector3 does not work 1 Answer

How to access position of prefab in the Game Object field? 1 Answer


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