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 /
  • Help Room /
avatar image
0
Question by Creative Inventory · Dec 16, 2015 at 09:27 PM · velocityknockback

Knockback and directions problem

I have this script attached to an object, if my player encounters that object it will be knocked back. I have six directions on which it will be knocked back. This image will show you the different direction of which my player may get knock back. My problem: X=0,Y=1 and X=0,Y=-1 doesn't work, however every other velocity works. How can I also include the directions of X=0,Y=1 and X=0,Y=-1. Thank you and here is my code:

 public class Knockback : MonoBehaviour {

 public float xForceToAdd;
 public float yForceToAdd;
 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     
 }
 void OnTriggerEnter2D(Collider2D other)  {
     if (other.gameObject.tag == "Player")
     {
         //Store the vector 2 of the location where the initial hit happened;
         Vector2 initialHitPoint = new Vector2 (other.gameObject.transform.position.x, other.gameObject.transform.position.y);
         float xForce = 0;
         float yForce = 0;
         //Grab our collided with objects rigibody
         Rigidbody2D rigidForForce = other.gameObject.GetComponent<Rigidbody2D>();
         //Determine left right center of X hit
         if (initialHitPoint.x > (this.transform.position.x + (this.transform.localScale.x /3)))
         {
             xForce = 1;
         }
         else if (initialHitPoint.x < (this.transform.position.x - (this.transform.localScale.x /3)))
         {
             xForce = -1;
         }
         else
         {
             xForce = 0;
         }
         if (initialHitPoint.y > (this.transform.position.y + (this.transform.localScale.y /3)))
         {
             yForce = 1;
         }
         else if (initialHitPoint.y < (this.transform.position.y - (this.transform.localScale.y /3)))
         {
             yForce = -1;
         }
         else
         {
             yForce = 0;
         }
         rigidForForce.velocity = new Vector2(xForce * xForceToAdd, yForce * yForceToAdd);
     }
     
 }

}

(http://noobtuts.com/content/unity/2d-pong-game/vector2_directions.png)

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 Soraphis · Dec 16, 2015 at 09:44 PM

In your image you show 8 knockback directions (9 possible in your code) :P

is your y value always zero? does the knockback in (1, 1) or (1, -1) work? is yForceToAdd set to something != 0?

you dont need both "else" cases (32-35, 44-47) because you set yForce and xForce to 0 in 19. and 20. your code seems to be correct, but im pretty sure there is a more elegant way of doing it.

Comment
Add comment · Show 2 · 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 Creative Inventory · Dec 18, 2015 at 01:58 PM 0
Share

So what should I do? $$anonymous$$y yForceToAdd it set to 100 and so is my xForceToAdd.

avatar image Creative Inventory Creative Inventory · Dec 18, 2015 at 02:17 PM 0
Share

Then what should I replace "else" with? because I tried taking it out the "else" statement but it didn't go well like at all. Also could you tell me the more elegant way of doing this?

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

33 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

Related Questions

Why is new Vector3 stronger when player is in the air? 0 Answers

2d knockback for fighting game 0 Answers

3d space heading/volocity direction prediction and mp/s hud,code? 1 Answer

Help with script for Bullet velocity 0 Answers

OnTriggerEnter2D at high velocity 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