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 WERFF · Mar 19, 2017 at 06:21 PM · physicscollision detectioncontrol

I can't jump sometimes

I can't jump sometimes when i land in another platform after jump. I not 100% sure how to use the funtion void OnTriggerEnter2D():

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMove : MonoBehaviour {

 public float MoveSpeed;
 public float JumpVelocity;
 private bool OnTheFloor;
 private Rigidbody2D RBody;


 // Use this for initialization
 void Start () {
     OnTheFloor = false;   
     RBody = GetComponent<Rigidbody2D>();
 
 }

 // Update is called once per frame
 void Update() {

     Movement();


 }

 void Movement() {
     if (Input.GetKey(KeyCode.RightArrow))
     {
         RBody.velocity = new Vector2(MoveSpeed, RBody.velocity.y);
         transform.eulerAngles = new Vector2(0, 0);
     }
    

     if (Input.GetKey(KeyCode.LeftArrow))
     {
         RBody.velocity = new Vector2(-MoveSpeed, RBody.velocity.y);
         transform.eulerAngles = new Vector2(0, 180);

     }
   
     if (Input.GetKey(KeyCode.LeftArrow)==false && Input.GetKey(KeyCode.RightArrow) == false) { 
         RBody.velocity = new Vector2(0f, RBody.velocity.y);
     }

   
     if (Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.UpArrow)) {
         if (OnTheFloor)
         {
             RBody.velocity = new Vector2(RBody.velocity.x, JumpVelocity);
         }
         
     }
     //evito que tenga velocidad cuando gira
     RBody.angularVelocity = 0f;

 }

 void OnTriggerEnter2D()
 {
     
     OnTheFloor = true;
 }

 void OnTriggerExit2D()
 {
    
     OnTheFloor = false;
 }

}

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 Commoble · Mar 19, 2017 at 06:28 PM

OnTriggerEnter2D is a message function; Unity's message system allows functions with known names to be called on components without an actual reference to that component. One downside is that due to details on how it works (which I won't go into here), your IDE (MonoDevelop, Visual Studio, etc), won't tell you if your function's name or arguments are incorrect, which is one of many reasons why you should always read the documentation on any built-in function or message before you use it.

As shown in the documentation for OnTriggerEnter2D in that link, this is how you should use them:

 void OnTriggerEnter2D(Collider2D other)
 {
     OnTheFloor = true;
 }
 
 void OnTriggerExit2D(Collider2D other)
 {
     OnTheFloor = false;
 }

The Collider2D argument can have any name (it doesn't have to be "other"), but the function has to have the Collider2D argument or Unity won't recognize it as a trigger message.

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Best collision detection method? 2 Answers

How do you detect collisions in DOTS Physics? 0 Answers

How do I "remove/disable" collision? 3 Answers

How do I get an asymmetrical spaceship to rotate correctly using torque? 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