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 /
avatar image
0
Question by CodePoet24 · Jul 06, 2016 at 07:29 PM · 2dattacktop-downknockbackrpg-game

2D RPG style knockback on attack

Hello everyone, I am developing a top down RPG and I have implemented an action attack system. The player can deal damage and recieve, but the problem is that there is no knowckback when the player or the enemy is getting attacked, this is the script of the player controller:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     Rigidbody2D rbody;
     Animator anim;
     GameObject attackingZone;
 
     public GameObject damageNumber;
 
     public bool canMove;
     public bool isAttacking;
     public Stats stats;
 
     public float attackRate;
     public float nextAttack;
 
     // Use this for initialization
     void Start () {
 
         rbody = GetComponent<Rigidbody2D>();
         anim = GetComponent<Animator>();
         attackingZone = transform.GetChild(0).gameObject;
         stats = GetComponent<Stats>();
         isAttacking = false;
         attackRate = 0.5f;
         nextAttack = 0.0f;
     }
     
     // Update is called once per frame
     void Update () {
 
         if(!canMove)
         {
             rbody.velocity = Vector2.zero;
             return;
         }
 
         Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
 
         if (movement_vector != Vector2.zero)
         {
             anim.SetBool("iswalking", true);
             anim.SetFloat("input_x", movement_vector.x);
             anim.SetFloat("input_y", movement_vector.y);
         }
         else
         {
             anim.SetBool("iswalking", false);
         }
 
         if(Input.GetKey(KeyCode.C) && Time.time > nextAttack)
         {
             anim.SetBool("isattacking", true);
             nextAttack = Time.time + attackRate;
             isAttacking = true;
         }
         else
         {
             isAttacking = false;
             anim.SetBool("isattacking", false);
         }
 
         rbody.MovePosition(rbody.position + movement_vector * Time.deltaTime);
 
     }
 }
 

And the enemy script: using UnityEngine; using System.Collections;

 public class SlimeController : MonoBehaviour {
 
     public float moveSpeed;
     public float timeBetweenMove;
     public float timeToMove;
 
     private Rigidbody2D rigidBody;
     public bool moving;
     private float timeBetweenMoveCounter;
     private float timeToMoveCounter;
     private Vector3 moveDirection;
     private Stats stats;
     private PlayerMovement player;
 
     // Use this for initialization
     void Start () {
 
         rigidBody = GetComponent<Rigidbody2D>();
         stats = GetComponent<Stats>();
         player = GameObject.Find("Player").GetComponent<PlayerMovement>();
 
         //timeBetweenMoveCounter = timeBetweenMove;
         //timeToMoveCounter = timeToMove;
 
         timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
         timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeBetweenMove * 1.25f);
     }
     
     // Update is called once per frame
     void Update () {
 
         Debug.Log(rigidBody.velocity.x);
 
         if(moving)
         {
             timeToMoveCounter -= Time.deltaTime;
             rigidBody.velocity = moveDirection;
 
             if(timeToMoveCounter < 0f)
             {
                 moving = false;
                 //timeBetweenMoveCounter = timeBetweenMove;
                 timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
             }
         }
         else
         {
             timeBetweenMoveCounter -= Time.deltaTime;
             rigidBody.velocity = Vector2.zero;
 
             if (timeBetweenMoveCounter < 0f)
             {
                 moving = true;
 
                 //timeToMoveCounter = timeToMove;
                 timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeBetweenMove * 1.25f);
 
                 moveDirection = new Vector3(Random.Range(-1f, 1f) * moveSpeed, Random.Range(-1f, 1f) * moveSpeed, 0f);
             }
         }
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if(other.gameObject.name == "Player")
         {
             other.gameObject.GetComponent<Stats>().currentHP -= stats.attack;
             var clone = (GameObject)Instantiate(player.damageNumber, other.transform.position, Quaternion.Euler(Vector3.zero));
             clone.GetComponent<FloatingNumbers>().damageNumber = stats.attack;
         }
     }
 }
 

Please help I really don't know how to do it.

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 Cherno · Jul 06, 2016 at 09:48 PM 1
Share

First problem is that you set the RB'S velocity to moveDirection or zero, so any kind of behavior simulated by the Physics engine is overridden. Secondly, to push a character back, use RigidBody.AddForce or RigidBody.AddExplosionForce.

avatar image CodePoet24 Cherno · Jul 07, 2016 at 03:43 PM 0
Share

I understand, but thats the way my enemies' moving work, or do I have to change that? So how can I make my enemies move without setting the RB's velocity to these vectors? And can you explain how can I use AddForce?

avatar image Cherno CodePoet24 · Jul 07, 2016 at 04:25 PM 0
Share

I just noticed that AddExplosionForce is not available for RigidBody2D, so you'd have to use one of the RB2D AddForce functions.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Anchored Rotation with Gradual Movement 0 Answers

2D Topdown Shooter Enemy Knockback 0 Answers

Event Trigger Pointer Exit sometimes not recognized 0 Answers

Trying to make my Player's sword knockback the enemy without making the enemy animation freak out. 0 Answers

2D Dash attack 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