• 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 Superfantasy · Jan 26, 2016 at 05:47 PM · 2draycastgrounded

2D raycast only returns true.

Hi everyone I'm new to unity scripting so sorry in advance if this is a noob problem. Anyway I have a 2dgame where I'm using ray-casts to see if a character is grounded if it is it should be able to jump once using space-bar or up arrow then cannot jump again until it is grounded here is my code.

 using UnityEngine;
 using System.Collections;
 [System.Serializable]
 public class twoDPlatformer : MonoBehaviour {
     
     public float jumpSpeed;
     public BoxCollider2D trigger;
     //public bool isGrounded;
     public Rigidbody2D target;
     public float Speed;
     public bool doubleJump;
     private PolygonCollider2D colide;
     private float distToGround;
     private int layermask;
 
     // Use this for initialization
     void Start()
     {
         trigger = GetComponent<BoxCollider2D>();
         target = GetComponent<Rigidbody2D>();
         //doubleJump = true;
         colide = GetComponent<PolygonCollider2D>();
         distToGround = colide.bounds.extents.y + 0.1f;
         layermask = 255 >> 1;
     }
         
     // Update is called once per frame
     void FixedUpdate () {
 
     }

     bool isGrounded()
     {
         return Physics2D.Raycast(transform.position, -Vector2.up, distToGround);
     }

     void Update()
     {
         /*if (Input.GetButton ("Right")) {
             target.velocity = new Vector3(Speed,target.velocity.y,0);
         }
 
         if (Input.GetButton ("Left")) {
             target.velocity = new Vector3(-Speed,target.velocity.y,0);
         }
         
         if (Input.GetButton ("Right") && Input.GetButtonDown ("Jump") && isGrounded) {
             target.velocity = new Vector3(Speed,jumpSpeed,0);
         }
         if (Input.GetButton ("Left") && Input.GetButtonDown ("Jump") && isGrounded) {
             target.velocity = new Vector3(-Speed,jumpSpeed,0);
         }
         if (Input.GetButtonDown ("Jump") && !isGrounded && doubleJump) {
             doubleJump = false;
             target.velocity = new Vector2(target.velocity.x,0);
             target.velocity = new Vector3(target.velocity.x,jumpSpeed,target.velocity.y);
             */
 
         //Ray2D myRay = new Ray2D(transform.position, -Vector2.up);
         //Debug.DrawRay(transform.position, -Vector2.up, Color.black);
         //Debug.Log(Physics2D.Raycast(transform.position, -Vector2.up));
         
         Debug.DrawRay(transform.position, -Vector2.up, Color.black);
   
         if (Input.GetButtonDown("Jump") && isGrounded())
         {
             target.velocity = new Vector2(target.velocity.x, jumpSpeed);
         }

         if(Input.GetButtonDown("Jump") && !isGrounded() && doubleJump)
         {
             target.velocity = new Vector2(target.velocity.x, jumpSpeed);
         }
     }
 }
 

My problem is the ray-cast only returns true and idk why and when i get it not to return true (which is difficult) it doesn't work properly.

Any help would me much appreciated.

P.S Sorry about the messy code.

Comment
Add comment · Show 2
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 incorrect · Jan 26, 2016 at 05:50 PM 0
Share

I guess it hits your players collider.

avatar image Superfantasy incorrect · Jan 27, 2016 at 02:13 PM 0
Share

How do i change this i tried using layer masks, but it seems I'm not doing it right

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Superfantasy · Jan 27, 2016 at 02:44 PM

Solved: I figured it out i had it hitting the collider on the character were all good now. Thank all that helped me figure out my problem.

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to freeze an GameObject on x axis in specific direction? 1 Answer

Raycast not detecting? 0 Answers

Physics2D.Raycast problems with vectors 1 Answer

Raycast Reflection 2D 1 Answer

Why GetPoint() returns always the same values ? 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