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
0
Question by RgClube · Sep 29, 2020 at 07:12 PM · velocitycollider2djumpingphysics.overlapsphere

How to fix the player flying into the sky after applying velocity on the y to him.

Hi guys, I want to kill enemies by jumping on them something similar to mario, but with enemies that do not die after the first jump. With this implementation, I ran into several problems, first my player flies into the stratosphere after touching an enemy, the second is that the enemies take more damage than they should (continuing to fall the player continues to inflict damage). By the way, I managed to solve each of these problems, but separately. In the attached version of the code, only the problem of damage is solved, but not with flying. Thank you in advance.

  MonoBehaviour
 {
   [SerializeField]
   private LayerMask whatIsDamageable;
   [SerializeField]
   private float stunAmount = 3;
   [SerializeField]
   private Transform stampPosition;
   [SerializeField]
   private float attack1Damage;
   [SerializeField]
   private float jumpForce = 10;
   public float attack1Radius = 5f;
 
   private AttackDetails attackDetails;
   private float currentHealth;
   private float health = 100;
   private GameObject player;
   Rigidbody2D rb;
   Collider2D detectedObject;
   private float timeBetweenStamp = 1f;
   private bool isEnemyDetected;
   private bool playerJumpAfterHitEnemy;
   private bool playerFinishedHisJump;
   PlayerMovement playerMovement;
   //animation
   private void Update() 
   {
       if(rb.velocity.y < 0){
       CheckAttackHitBox();
       }
       
       ifStampedEnemy();
       
       
   }
   private void Start() 
   {
       rb = GetComponent<Rigidbody2D>();
       playerMovement = GetComponent<PlayerMovement>();
       currentHealth = health;
       player = GameObject.Find("Player");
   }
  
 
   private void CheckAttackHitBox()
   {
 
       isEnemyDetected = Physics2D.OverlapCircle(stampPosition.position, attack1Radius, whatIsDamageable);
       detectedObject = Physics2D.OverlapCircle(stampPosition.position, attack1Radius, whatIsDamageable);
       attackDetails.damageAmount = attack1Damage;
       attackDetails.position = transform.position;
       attackDetails.stunDamageAmount = stunAmount;
       if(detectedObject != null && isEnemyDetected == true){
       detectedObject.transform.parent.SendMessage("Damage", attackDetails);
       timeBetweenStamp -= Time.deltaTime;
       }
       if(timeBetweenStamp <=0)
       {
           isEnemyDetected = false;
           timeBetweenStamp = 2f;
       }
           // sendmeesage func is used to call a specific func in a script of an object
           //Instantiate hit particle 
       
   }
   private void ifStampedEnemy()
   {
       if(isEnemyDetected)
       {
           rb.velocity = new Vector2 (rb.velocity.x , jumpForce);
       }
   }
   private void Damage(AttackDetails attackDetails)
   {
       currentHealth -= attackDetails.damageAmount;
   }
   private void OnDrawGizmos()
   {
       Gizmos.DrawWireSphere(stampPosition.position, attack1Radius);
   }
 }
 `






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

Answer by rh_galaxy · Sep 29, 2020 at 11:40 PM

Your use of timeBetweenStamp will not work since you only count it down when you hit an enemy from above (one frame), it will not reach zero in a predictable way.

And isEnemyDetected will continue to be true until you are falling again, and since you set rb.velocity.y to jumpForce every frame that will never happen - you will fly away.

This could work better, and if you still want a minimum timeBetweenStamp you can add that.

 private void Update() 
 {
     if(rb.velocity.y < 0) {
         CheckAttackHitBox();
     }
 }
 private void CheckAttackHitBox()
 {
     detectedObject = Physics2D.OverlapCircle(stampPosition.position, attack1Radius, whatIsDamageable);
     if(detectedObject != null)
     {
         attackDetails.damageAmount = attack1Damage;
         attackDetails.position = transform.position;
         attackDetails.stunDamageAmount = stunAmount;
         detectedObject.transform.parent.SendMessage("Damage", attackDetails);
         rb.velocity = new Vector2 (rb.velocity.x , jumpForce);
     }
 }
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

141 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

Related Questions

Error when trying to implement JumpPad/GravityPad type effect on CharacterController (Solved) 1 Answer

Velocity doesnt change properly 1 Answer

Why is enemy jumping at different speed even though the velocity is the same? 0 Answers

Why does Gravity (Physics2D) produce different results for AddForce() versus velocity in player sprite? 2 Answers

2D Charachter infinite jumping 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