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 Emolk · May 05, 2014 at 12:39 PM · 2daddforceknockback

How to apply knockback on collision (2D)

Here is the code for my player movement:

 function Update () {
 
         animator.SetInteger("Direction", 4);
 
     if (Input.GetKey(moveUp) && isGrounded == 0)
     {
         rigidbody2D.velocity.y = jumpheight;
         isGrounded = 1;
         animator.SetInteger("Direction", 2);
     }
     
     
     else if (Input.GetKey(moveLeft))
     {
         transform.position += Vector3.left * speed * Time.deltaTime;
         animator.SetInteger("Direction", 3);
     }
     else if (Input.GetKey(moveRight))
     {
         transform.position += Vector3.right * speed * Time.deltaTime;
         animator.SetInteger("Direction", 1);
     }
     
 }






And here is the code for the collision:

 var PlayerHealth = 100.0;
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function OnCollisionEnter2D(coll: Collision2D) {
          
         if (coll.gameObject.tag == "Ghost"){
         PlayerHealth = PlayerHealth - 5.00;
         rigidbody.AddForce (Vector3.left * 10);
         }
         
 
 }







The add force has no effect on my character. The collision works though, as the players health goes down. I'm pretty certain it has to do with the transform position in my movement, but I'm not sure how to fix it. Any help?

Comment
Add comment · Show 4
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 Pyrian · May 05, 2014 at 03:13 PM 0
Share

Hmm, yes. You cannot combine physics with hard-setting the position - the position set will simply override any forces applied. You can either re-write your left-right movement to use physics forces, or re-write your knock back to NOT use physics forces, or, here's a nice thought, re-use your jump code by making the knockback hit you back AND UP, setting your isGrounded appropriately.

avatar image Emolk · May 06, 2014 at 12:17 PM 0
Share

How would i do any of those? I just copied a youtube tutorial on the movement. How would i make the knock back not use physics but still be a knockback? Or how would i move left and right smoothly without fixing position? (I would rather do the latter as it allows me to put in other effects as well)

avatar image andyspeak · May 06, 2014 at 02:06 PM 0
Share

addforce can be a pain i always but a big number in add force like 10000 so i know for a fact it has no effect but emolk is right too i think try what i sed you might get lucky

avatar image Emolk · May 06, 2014 at 11:12 PM 0
Share

Could anybody suggest a way to change the movement of my character to use physics?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tetigi · May 07, 2014 at 02:57 PM

As Pyrian said, you cannot combine the physics engine whilst hard-setting the position. However, you can turn the physics engine on and off by setting the RigidBody.isKinematic variable.

Whilst this is on, you can set your position manually as before, and when you want the physics to take over (such as when a collision happens), you turn it off and make sure that you do not make any more position updates until it's back on again. The physics engine should take care of the rest.

Looking at your code, perhaps you could wrap that in an if statement that checks whether or not it is currently kinematic - if it is, do standard control stuff. If it's off, you'll need to wait until your collision is 'finished' before it turns back on.

Maybe something like:

 function Update() {
   if (rigidbody2D.isKinematic) {
     // Do Input key positional stuff
   } else {
     // Wait for collision to finish
     if (collisionIsFinished()) {
       rigidbody2D.isKinematic = true;
     }
   }
   
   // other things
 }

 bool collisionIsFinished() {
   // code that checks when your collision is done or whatever
 }

and then in the player code, add one simple change:

 function OnCollisionEnter2D(coll: Collision2D) {
 
   if (coll.gameObject.tag == "Ghost"){
     PlayerHealth = PlayerHealth - 5.00;
     rigidbody.AddForce (Vector3.left * 10);
     rigidbody2D.isKinematic = false;
   }

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

23 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

Related Questions

Deteriorating force on object? 0 Answers

How do I add diagonal force? 0 Answers

Uneven speed in 2d movement script 2 Answers

knockback 3d c# 1 Answer

Rigidbody2D x velocity not moving unless placed in FixedUpdate 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