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 Ellis · Jan 28, 2015 at 08:19 AM · forcepushdashknockback

2D dash/knockback?

So basicly what Im trying to do is a knockback effect for my 2D fighting game, however I only seem to be able to make the knockedback player teleport back instead of being smoothly pushed back. Any help?

alt text

 using UnityEngine;
 using System.Collections;
 
 public class playerFighting : MonoBehaviour {
     public int health = 1000;
 
     public float initialDelay;
     public float afterDelay;
     public float attackDelay;
     public float shurikenSpeed;
 
     public bool isAttacking = false;
     public bool inRange = false;
     public bool isHit = false;
 
     public int attackStacks = 0;
 
     playerMovement pm;
     gameManager gm;
 
     GameObject target;
 
     new string name;
 
     bool shurikenDelay = false;
 
     Animator anim;
 
     // Use this for initialization
     void Start () {
         name = this.gameObject.name;
         anim = GetComponent<Animator> ();
         pm = GetComponent<playerMovement> ();
 
     }
     
     // Update is called once per frame
     void Update () {
         //Attack
         if (Input.GetAxis (name + " Attack") == 1 && !isAttacking && pm.jumpEnable > 0) {
             StartCoroutine(Attack());
         }
 
         //Shuriken
         if((Input.GetAxis(name + " ShurikenX") >= 0.5 || Input.GetAxis(name + " ShurikenY") >= 0.5 || Input.GetAxis(name + " ShurikenX") <= -0.5 || Input.GetAxis(name + " ShurikenY") <= -0.5) && !shurikenDelay){
             StartCoroutine(Shuriken());
         }
 
         //Health
         if (health <= 0) {
             Debug.Log (name + " lost. Game Over!");
             gm.GameOver();
         }
 
         anim.SetFloat("Attack", Input.GetAxis(name + " Attack"));
         anim.SetInteger ("AttackStacks", attackStacks);
         anim.SetBool ("isHit", isHit);
     }
 
 
     IEnumerator Attack(){
         attackStacks = 0;
 
         isAttacking = true;
         yield return new WaitForSeconds (initialDelay);
         //Debug.Log (name + " is attacking.");
         if (inRange) {
             while(Input.GetAxis(name + " Attack") == 1){
                 float timer = Time.time;
                 float reset = timer + 1;
 
                 if(attackStacks < 2){
                     target.GetComponent<playerFighting>().isHit = true;
                     target.GetComponent<playerFighting> ().health -= 10;
                     yield return new WaitForSeconds (attackDelay);
                     attackStacks++;
                     target.GetComponent<playerFighting>().isHit = false;
                     yield return new WaitForSeconds (initialDelay);
                     continue;
                 }
 
                 else if(attackStacks == 2){
                     if(target !=null){
                         Debug.Log("Power attack!");
                         target.GetComponent<playerFighting> ().health -= 20;
                         target.rigidbody2D.AddForce(new Vector2(-this.transform.localScale.x * 10000, 1) * 100 * Time.deltaTime * 0.5f);
                     }
                     //powerAttack
                     break;
                 }
 
                 timer += 1 * Time.deltaTime;
 
                 if(timer == reset){
                     attackStacks = 0;
                     break;
                 }
             }
             if(target != null)
                 target.GetComponent<playerFighting>().isHit = false;
         }
         if(target != null)
             target.GetComponent<playerFighting>().isHit = false;
         yield return new WaitForSeconds (afterDelay);
         attackStacks = 0;
         isAttacking = false;
     }
 
     IEnumerator Shuriken(){
         shurikenDelay = true;
         GameObject Shuriken = (GameObject)Instantiate(Resources.Load ("Shuriken"));
         Shuriken.GetComponent<shuriken>().Player = this.gameObject;
         Shuriken.GetComponent<shuriken>().player = this.rigidbody2D;
         Shuriken.transform.position = new Vector2(this.transform.position.x, this.transform.position.y + 0.1f);
         //shuriken = Shuriken;
         Shuriken.rigidbody2D.AddForce(new Vector2(Input.GetAxis(name + " ShurikenX"), Input.GetAxis(name + " ShurikenY")) * shurikenSpeed);
         yield return new WaitForSeconds (1.5f);
         shurikenDelay = false;
     }
 
     void OnTriggerEnter2D(Collider2D other){
         if (other.tag == "Player") {
             Debug.Log("In range");
             target = other.gameObject;
             inRange = true;
         }
     }
 
     void OnTriggerExit2D(Collider2D other){
         if (other.tag == "Player") {
             Debug.Log("out of range");
             target = null;
             inRange = false;
         }
     }
 }
 
untitled-1.jpg (326.6 kB)
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 HarshadK · Jan 28, 2015 at 08:29 AM

If there is a rigidbody on your pushback player then you can use Rigidbody.AddForce and let physics engine do its task.

If not, then you need to use Vector3.Lerp or Vector3.MoveTowards to move your player rather than instantly teleporting him.

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 Ellis · Jan 28, 2015 at 08:32 AM 0
Share

It has a rigidbody (2D) however addforce seems to teleport the player anyways or not push at all.

avatar image HarshadK · Jan 28, 2015 at 08:44 AM 0
Share

Can you share your code along with how your player object is set up (inspector screenshot for player object)?

avatar image Ellis · Jan 28, 2015 at 09:09 AM 0
Share

there you go! A picture and a wall of code :P

avatar image Ellis · Jan 28, 2015 at 09:10 AM 0
Share

Oh, and the part I need help with is at row 86.

avatar image HarshadK · Jan 28, 2015 at 10:30 AM 0
Share

You are applying regular force in your coroutine which seems to be the issue. Try applying a Force$$anonymous$$ode.Impulse and set the value of the force applied to be as required.

 target.rigidbody2D.AddForce(new Vector2(-this.transform.localScale.x * 10000, 1) * 100 * Time.deltaTime * 0.5f, Force$$anonymous$$ode.Impulse);

Also start with small values rather than using 1000 and 100.

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

19 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

Related Questions

Tricky issue involving knockback and the player. 1 Answer

How to make Jedi Push and Pull objects (video) 2 Answers

Adding 1 frame of force 2 Answers

How to move Rigidbody2D on Player a certain amount at a certain speed, for a dash move and knockback? 2 Answers

pulling objects across a collider 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