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 /
avatar image
1
Question by dodongoo · Apr 22, 2014 at 04:11 PM · 2dmovementraycastplatformer

2D jump with Raycast

Hello everyone, I'm new to Unity and programming and I'm doing my first 2D platformer.

I'm writing my own script for the player to move and I've got a problem with the raycast. I've got two cubes: the floor that has got a box collider 2D, and the player that's got a box collider 2D and a rigidbody 2D.

What I want to do is to detect the floor using the raycast and make the player able to jump then. The problem is the raycast seems to be active all the time so it actually doesn't change anything. When I delete the "Input.GetKeyDown(KeyCode.Space)" then the character just flies away. Here's the whole code:

     var speed : float = 10F;
     var jumpSpeed : float = 10F;
 
 function Update() {
 
     transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0, Space.World);
     
     var hit : RaycastHit2D = Physics2D.Raycast(transform.position, -Vector2.up);
     
     if((hit != null) && (Input.GetKeyDown(KeyCode.Space))) {
     
             Jump();
             
     }
     
 }
 
 function Jump() {
 
     rigidbody2D.velocity = new Vector2(0, jumpSpeed);
 
 }

I was looking for the answer for a very long time and checked a lot of possibilities - nothing works.

Sorry for my English if I made any mistakes

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
1
Best Answer

Answer by koray1396 · Apr 22, 2014 at 04:28 PM

Try this;

 float someValue = 0.1f;
 Physics2D.Raycast(transform.position, -Vector2.up, someValue);

you can set a maximum distance of the ray to check if the player is on ground.

Comment
Add comment · Show 3 · 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 dodongoo · Apr 22, 2014 at 04:52 PM 0
Share

Hmm, nothing changed, the ray's active all the time. Any other ideas?

avatar image koray1396 · Apr 22, 2014 at 05:25 PM 0
Share

oops, this also;

public Layer$$anonymous$$ask GroundLayer;

you should select the layer of ground only, it is possibly detecting player itself.

avatar image dodongoo · Apr 23, 2014 at 07:58 PM 0
Share

Thanks a lot. You really heleped me.

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

21 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Questions about movement on uneven/slopped terrain and terrain coliders 0 Answers

2D Platform Player moving instantly from upper platform to lower, only when moving left 0 Answers

How to prevent jitter with movement over curved slopes 0 Answers

Help setting maxAceleration 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