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 MrSpuriz · Mar 11, 2016 at 01:59 AM · 2d2d gamephysics2daddforceknockback

Help with 2D topdown knockback

Hello! i am having a problem with my 2D topdown game, i want to make a slime knockback the player, and i can´t do it work like i want to, i want it to knockfrom right to the left, up to down ETC. but it only knocks from right to left and left to right, i have tried many things i will add the playermovement and the hurtplayer script, also the AI script. AI:

 using UnityEngine;
 using System.Collections;
 
 public class AI : MonoBehaviour {
     
     private bool attacking;
     public float attackTime;
     private float attackTimeCounter;
     private Vector3 Player;
     private Vector2 Playerdirection;
     private float Xdif;
     private float Ydif;
     private float speed;
     private int Wall;
     private float distance;
     private float distanceFromPlayer;
     Rigidbody2D rb;
     public float Range = 5;
     public float Speed;
     Animator anim;
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody2D> ();
         Wall = 1 << 8;
         speed = Speed;
         anim = GetComponent<Animator> ();
     
     }
     
     // Update is called once per frame
     void Update () {
 
         distance = Vector2.Distance (Player, transform.position);
         Player = GameObject.Find ("Player").transform.position;
 
         if (distance < Range) {
 
             Xdif = Player.x - transform.position.x;
             Ydif = Player.y - transform.position.y;
             Playerdirection = new Vector2 (Xdif, Ydif);
 
             if (!Physics2D.Raycast (transform.position, Playerdirection, 5, Wall)) 
             {
                 rb.AddForce (Playerdirection.normalized * speed);
             }
         }
     }
 }

player movement

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     Rigidbody2D rbody;
     Animator anim;
 
     private bool attacking;
     public float attackTime;
     private float attackTimeCounter;
 
     public float knockBack;
     public float knockBackCount;
     public float knockBackLength;
     public bool knockFromRight;
     public bool knockFromUp;
     public bool knockFromLeft;
     public bool knockFromDown;
 
     // Use this for initialization
     void Start () {
 
         rbody = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
 
     }
     
     // Update is called once per frame
     void Update () {
         if (knockBackCount <= 0) {
             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);
             }
 
             rbody.MovePosition (rbody.position + movement_vector * Time.deltaTime);
             {
                 if (Input.GetKeyDown (KeyCode.J)) {
                     attackTimeCounter = attackTime;
                     attacking = true;
                     anim.SetBool ("isattacking", true);
                 }
                 if (attackTimeCounter > 0) {
                     attackTimeCounter -= Time.deltaTime;
                 }
                 if (attackTimeCounter <= 0) {
                     attacking = false;
                     anim.SetBool ("isattacking", false);
                 }
             }
         } else {
             if (knockFromRight)
                 rbody.velocity = new Vector2 (-knockBack, 0f);
             else
                 knockFromRight = false;
             if (knockFromLeft)
                 rbody.velocity = new Vector2 (knockBack, 0f);
                     else
                 knockFromLeft = false;
             if (knockFromUp)
                 rbody.velocity = new Vector2 (0f, -knockBack);
             else
                 knockFromUp = false;
             if (knockFromDown)
                 rbody.velocity = new Vector2 (0f, knockBack);
             else
                 knockFromDown = false;
             
             knockBackCount -= Time.deltaTime;
         }
             if (knockBackCount <= 0)
             {
                 knockFromRight = false;
                 knockFromLeft = false;
                 knockFromUp = false;
                 knockFromDown = false;
             knockBackCount = 0;
             }
         }
     }

hurtplayer

 using UnityEngine;
 using System.Collections;
 
 public class HurtPlayer : MonoBehaviour {
 
     public int damageToGive;
     public GameObject damageNumber;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnCollisionEnter2D (Collision2D other)
     {
         if (other.gameObject.name == "Player") {
             
             other.gameObject.GetComponent<PlayerHealthManager> ().HurtPlayer (damageToGive);
             var clone = (GameObject)Instantiate (damageNumber, other.transform.position, Quaternion.Euler (Vector3.zero));
             clone.GetComponent<FloatingNumbers> ().damageNumber = damageToGive;
         }
     }
 
         void OnTriggerEnter2D(Collider2D other)
         {
         var player = other.GetComponent<PlayerMovement> ();
         player.knockBackCount = player.knockBackLength;
 
         if (other.gameObject.name == "Player") {
 
             if (other.transform.position.x < transform.position.x)
                 player.knockFromRight = true;
             if (other.transform.position.x > transform.position.x)
                 player.knockFromLeft = true;
             if (other.transform.position.y < transform.position.y)
                 player.knockFromUp = true;
             if (other.transform.position.y > transform.position.y)
                 player.knockFromDown = true;
         }
         if (player.knockFromRight == true) 
         {
             player.knockFromDown = false;
             player.knockFromUp = false;
              player.knockFromLeft = false;
         }
         if (player.knockFromLeft == true)
         {
             player.knockFromDown = false;
             player.knockFromUp = false;
             player.knockFromRight = false;
         }
 
             if (player.knockFromUp == true)
         {
                 player.knockFromDown = false;
                 player.knockFromRight = false;
                 player.knockFromLeft = false;
         }
         if (player.knockFromDown == true) 
         {
             player.knockFromRight = false;
             player.knockFromUp = false;
             player.knockFromLeft = false;
         }
             
     }
   }



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

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

74 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 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

2D Knockback problem 2 Answers

Arkanoid-like bounce troubles 1 Answer

How do I make Polygon Collider 2D more smooth? 0 Answers

Trying to add force to an object... is this done correctly? 0 Answers

How to make a knocback smooth - 2D Unity 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