Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 gotocase1 · Aug 01, 2015 at 06:59 AM · raycastjumpjumpingisgrounded

Raycast2D Only returns true

I'm bulding a simple 2D platformer but whenever I raycast it only returns true. Even when there is no object as i used Debug.DrawRay and saw the ray was hitting nothing but still the raycast was still returning true even as my player is under all of the Coliders.

 using UnityEngine;
 using System.Collections;
 
 public class Controls : MonoBehaviour {
     [HideInInspector] public bool FacingRight;
     public float Speed = 6f;
     public float RunSpeed = 12f;
     public float JumpDistance = 1f;
     Animator playerControls;
     Collider2D playerBox;
     Rigidbody2D playerRigidbody;
     Vector2 positonToMove;
     float distToGround;
 
     void Start () {
         playerControls = GetComponent<Animator> ();
         playerRigidbody = GetComponent<Rigidbody2D> ();
         playerBox = GetComponent<BoxCollider2D> ();
         distToGround = playerBox.bounds.extents.y;
     }
 
     void Update () {
         float x = Input.GetAxisRaw ("Horizontal");
         Move (x, 0);
         if (Input.GetKey("z")  && IsGrounded() == true ) 
         {
             Jump();
         }
         else if(Input.GetKey("x"))
         {
             Debug.Log(IsGrounded().ToString());
         }
         Animate (x,0);
     }
     void Move(float x, float y)
     {
         positonToMove.Set (x, y);
          if (playerControls.GetBool ("IsRunning") == true) {
             Speed = RunSpeed;
         } 
         else {
             Speed = 6f;
         }
         positonToMove = positonToMove.normalized * Speed * Time.deltaTime;
         playerRigidbody.MovePosition ((Vector2)transform.position + positonToMove);
     }
     void Jump()
     {
         playerRigidbody.AddForce(new Vector2(0f, JumpDistance* 5));
     }
     void Animate(float x, float y)
     {
         bool isWalking = x != 0f;
         bool isRunning = x == 1f || x == -1f;
         bool isJumping = y != 0f;
         bool isFalling = playerRigidbody.velocity.y < -0.1f;
         playerControls.SetBool ("IsWalking", isWalking);
         playerControls.SetBool ("IsRunning", isRunning);
         playerControls.SetBool ("IsJumping", isJumping);
         playerControls.SetBool ("IsFalling", isFalling);
     }
     bool IsGrounded()
     {
         RaycastHit2D myHit;
         int Layermask = 1 << 8;
         Layermask = ~Layermask;
         Debug.DrawRay(transform.position, -(Vector2)Vector3.up, Color.red, 0.1f );
         bool p = Physics2D.Raycast ((Vector2)transform.position, -Vector2.up, /*distToGround*/ 0.1f, Layermask);
         if (p == true) {
             Debug.Log ("T");
         } else {
             Debug.Log("F");
         }
         return p;
     }
 }

Comment
Add comment · Show 3
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 gotocase1 · Jul 31, 2015 at 10:15 PM 0
Share

thank you for telling me how to do that i should have realized the ones and zeros were for highlighting code

avatar image Priyanshu · Aug 01, 2015 at 07:11 AM 0
Share

ray can return the object's collider( from which its cast, if cast from a point inside of that collider). Have you ignored that collider?

avatar image gotocase1 · Aug 01, 2015 at 11:41 AM 0
Share

I might have missused the bitwise shift and reversed it saying check only layer 8 when I should have said simply shift 8

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nullgobz · Aug 01, 2015 at 12:05 PM

Your testing if the RaycastHit2D is not null and it never is. Try this:

 RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 0.1f, Layermask);
 if(hit.collider != null)
 {
    print("HIT");
 }

About bitswift, yeh your checking against all layers except for layer 8. And if layer 8 is ur character this makes sense so you dont check against yourself.

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 gotocase1 · Aug 03, 2015 at 07:53 PM 0
Share

Ya I just realized that right after I posted it and thanks I will try that

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Still constant jumping even with raycasting. 0 Answers

ground check isnt working...HELP! 2 Answers

Player Inconsistent Jumps 0 Answers

Player sticks to ground occasionally after trying to jump 0 Answers

Anti-Gravity isn't working 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