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 Lucario3013 · May 17, 2020 at 06:00 AM · 2dplatformerlocalscaleknockback

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

I have a patrol type enemy for my 2D platformer and I am trying to make the enemy knock back when I hit it with a sword. Right now, I am using AddForce, but because of the way my Enemy movement is setup, its localScale and moveRight gets messed up causing the Enemy to flip really fast between scale x = 1 and -1. I need help finding a work around for this.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerHit : MonoBehaviour
 {
     public int meleeDamage;
 
     public int thrust;
 
     private PlayerController Player;
 
     public GameObject enemyDeathEffect;
     public GameObject hitEffect;
 
     // Start is called before the first frame update
     void Start()
     {
         Player = GetComponentInParent<PlayerController>();
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.gameObject.CompareTag("Enemy"))
         {
             Rigidbody2D enemy = collision.GetComponent<Rigidbody2D>();
             if(enemy != null)
             {
                 enemy.AddForce(new Vector2(0, 1) * thrust, ForceMode2D.Impulse);
 
             }
 
             Instantiate(enemyDeathEffect, collision.transform.position, collision.transform.rotation);
             collision.gameObject.GetComponent<EnemyHealth>().TakeDamage(meleeDamage);
 
         }
 
     }
 
 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyPatrol : MonoBehaviour
 {
     public float moveSpeed;
     public bool moveRight;
 
 
     private Rigidbody2D myRigidbody;
     private Animator anim;
     private EnemyController enemyController;
 
     public Transform wallDetection;
     private float wallRadius = 0.01f;
     public LayerMask whatIsWall;
     private bool hittingWall;
 
     private bool notAtEdge;
     public Transform edgeDetection;
 
 
     // Start is called before the first frame update
     void Start()
     {
         myRigidbody = GetComponent<Rigidbody2D>();
         anim = GetComponent<Animator>();
         enemyController = GetComponent<EnemyController>();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         WallCheck();
         EdgeCheck();
 
         if (hittingWall || !notAtEdge)
             moveRight = !moveRight;
 
         MoveEnemy();
 
 
     }
 
 
     public void MoveEnemy()
     {
         if (myRigidbody.velocity != Vector2.zero)
             anim.SetBool("walk", true);
         else
             anim.SetBool("walk", false);
 
         if (moveRight)
         {
             myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y);
 
             transform.localScale = new Vector2(1, 1);
 
         }
         else
         {
             myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y);
             transform.localScale = new Vector2(-1, 1);
 
         }
 
     }
 
 
         void WallCheck()
         {
             Vector2 wallDetect = new Vector2(wallDetection.position.x, wallDetection.position.y);
             hittingWall = Physics2D.OverlapCircle(wallDetect, wallRadius, whatIsWall);
         }
 
         void EdgeCheck()
     {
         Vector2 edgeDetect = new Vector2(edgeDetection.position.x, edgeDetection.position.y);
         notAtEdge = Physics2D.OverlapCircle(edgeDetect, wallRadius, whatIsWall);
     }
 
 
 }
 
 
 
 
 
 
 
 

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

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

animation 2d platformer 2 Answers

issue with the rigid body x axis ,addforce doesn't work as it should for x axis 1 Answer

Cinemachine camera weird movement on Windows build 0 Answers

2D Flashlight for a platformer 2 Answers

Velocity from external objects 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