Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by EpicCrownKing · Jul 23, 2019 at 04:36 PM · 2draycastphysics2dphysics2d.raycast

2D Raycasting to check if grounded not working

I'm currently working on optimizing an old game project of mine, completely redoing the controls, improving graphics, etc. Originally, I had been using an excessive amount of colliders to check if the player was grounded(to avoid infinite jumping) but during optimization, I decided to use raycasts. I can't figure out why my (C#) code isn't working, and even though I saw many different questions with answers on the topic, they weren't very helpful to me. Player Script with Raycasting Code: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class PplayerScript : MonoBehaviour
 {
     //Variables
     public int movementSpeed;
     public int jumpHeight;
     public Rigidbody2D rb;
     public SpriteRenderer SpriteRenderer;
     public bool canJump;
     public RaycastHit hit;
 
     void Update()
     {
         //Player controls
         if (Input.GetKey("a"))
         {
             transform.Translate(Vector3.left * Time.deltaTime * movementSpeed);
             SpriteRenderer.flipX=true;
         }
         if (Input.GetKey("d"))
         {
             transform.Translate(Vector3.right * Time.deltaTime * movementSpeed);
             SpriteRenderer.flipX = false;
         }
 
         //Raycasting
         Vector2 fwd = transform.TransformDirection(Vector2.up);
         Debug.DrawRay(transform.position, fwd * -0.55f, Color.red);
         if (Physics.Raycast(transform.position, fwd * -0.55f) && hit.transform.gameObject.tag == "Ground")
         {
             Debug.DrawRay(transform.position, fwd * -0.55f, Color.green);
             Debug.Log("lel");
             canJump = true;
         }
         else
         {
             Debug.DrawRay(transform.position, fwd * -0.55f, Color.blue);
             canJump = false;
         }
 
         //Checking canJump
         if (Input.GetKey("space") && canJump==true)
         {
             rb.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
         }
     }
 }

Any ideas? Thanks, EpicCrownKing

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 I_Am_Err00r · Jul 23, 2019 at 05:36 PM

I want to try and help, but can you tell me where it gives you errors? What Debug.Logs are coming back as true or giving you the correct answers?


I don't have a compiler, am just typing this at my call center job right now, so I can't test, but I do see you don't have any variables defining the raycast sizes, hardcoding those values probably isn't the best solution, and maybe making something public and setting it in the inspector might be the best way to go until you find a more elegant solution that can define it exactly every time.


This video on youtube has a 2D raycasting tutorial that you should check out.

Sorry I couldn't give you the golden solution, but raycasting has to be very precise for it to work all the time, and if you don't have it setup correctly, it will give you a lot of headaches.

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 EpicCrownKing · Jul 23, 2019 at 10:00 PM 0
Share

Well, thanks for the help! I'm not actually getting any errors, the player simply isn't jumping. I am debugging the raycast, which always shows up as blue. I'll try assigning the raycast size to a singular variable within the code. I'll check out that video and get back to ya. -EpicCrown$$anonymous$$ing

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

248 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 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

Bitmask/LayerMask used in raycast not working 2 Answers

Physics2D Raycast not detecting wall colliders 1 Answer

Raycast ricochet only works for a couple of Rays 1 Answer

How can I get the rotation of Handles.DrawLine to match up with an Objects forward? 0 Answers

Is it possible to raycast during edit mode? 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