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 21, 2015 at 11:08 AM · c#directionvector2bounce

How to create a Knockback vector2 directions script (c#)

I have this topdown game on which I want to create a knockback effect when my player collides with my object. I want to create a knock back script using OnTriggerEnter2D. I want the knock back script to look like this. How Would I do that? (sorry I'm a bit of a noob) (http://noobtuts.com/content/unity/2d-pong-game/vector2_directions.png)

Comment
Add comment · Show 1
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 NoseKills · Dec 21, 2015 at 11:39 PM 0
Share

What have you tried ? What didn't work ? Where did you run into trouble? Did you already search and look at these answers ?

Is your OnTriggerEnter() firing correctly or should that be fixed first? If so, there's plenty of questions and answers covering that on this site too.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Firedan1176 · Dec 22, 2015 at 02:14 AM

How are you checking to see where the object is? If the object is in front, you should push the player the other way. One way to do this would be to get the difference of the player's location and the object's location. Then, you can get that as a 1 or a -1, and add that as a force. Something like this:

 public class BounceBack : MonoBehaviour {
 
     public GameObject theObject;             //The object you collide with.
     public Vector2 bounceIntensity;        //The amount of force applied to the player.
 
     void Start() {
         if(theObject == null)
             Debug.Log("Object isn't set!");
     }
     
     void OnTriggerEnter2D(Collider2D col) {
         Vector2 bounceBack = transform.position - theObject.transform.position;        //This should just "Chop off" the Z coordinate.
 
         bounceBack = new Vector2((bounceBack.x > 0) ? 1 : -1, (bounceBack.y > 0) ? 1 : -1)
         GetComponent<Rigidbody2D>().AddForce(bounceBack * bounceIntensity, ForceMode2D.Impulse);
         
     }    
 }


Now I haven't tested this, but it should work. Leave a comment if it doesn't give you the result you want.

**EDIT*: I have replaced object with theObject, since object is a predefined word, and cannot be a variable name. Sorry about that!

Comment
Add comment · Show 5 · 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 22, 2015 at 10:02 AM 0
Share

Thank you for replying back but I'm getting four errors in my console these are:

(18,44): error CS1525: Unexpected symbol `GetComponent'

avatar image Creative Inventory Creative Inventory · Dec 22, 2015 at 10:08 AM 0
Share

(10,36): error CS1525: Unexpected symbol ==', expecting .'

avatar image Creative Inventory Creative Inventory · Dec 22, 2015 at 10:08 AM 0
Share

(6,41): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration

Show more comments

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

43 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

Related Questions

How to get directional words with vector2? 0 Answers

Struggling with bounce pad in platformer game. 1 Answer

Object rotates normaly while moving, then does zig zags? 0 Answers

Lerping at a constant rate? 2 Answers

Bounce between two exact points 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