Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Alphacreations · May 24, 2020 at 07:17 AM · knockback

object always "knockbacking" wrong way?

Hello fellow unity members,

I made a punching bag in my game and it registers the wrong movement

 using UnityEngine;
 
 public class EnemyStuff : MonoBehaviour
 {
     public Animator animator;
     public int maxHealth = 300;
     int currentHealth;
     private Rigidbody2D rb;
 
     public LookAtCharacter LookAtCharacter;
 
 
 
 
     void Start()
     {
         currentHealth = maxHealth;
         rb = GetComponent<Rigidbody2D>();
         LookAtCharacter = GameObject.Find("Punching bag").GetComponent<LookAtCharacter>();
 
         
 
    
 
     }
 
 
 
     public void TakeDamage(int damage)
     {
         currentHealth -= damage;
 
         // play hurt animaton
         if (damage == 10)
         {    
             animator.SetTrigger("jab");
             if (LookAtCharacter.left == false)
             {
                 rb.AddForce(new Vector2(150, 0));
             }
             else if (LookAtCharacter.right == false)
             {
                 rb.AddForce(new Vector2(-150, 0));
             }
             
             
         }

} }

I have referenced the left and right boolean from this script

 public class LookAtCharacter : MonoBehaviour
 {
     public bool facingRight = false;
     private Transform target;
     public float speed;
     public bool right;
     public bool left;
     public void Start()
     {
         target = GameObject.FindGameObjectWithTag("Player").transform;
         left = true;
         right = false;
     }
     private void Flip()
     {
         Vector3 scale = transform.localScale;
         scale.x *= -1;
         transform.localScale = scale;
         facingRight = !facingRight;
 
     }
 
     public void Update()
     {
         right = target.position.x > transform.position.x && !facingRight;
         left = target.position.x < transform.position.x && facingRight;
 
             if (Vector3.Distance(target.position, transform.position) < 600)
         {
 
             transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
 
             if (right == true) //if the target is to the right of enemy and the enemy is not facing right
                 Flip();
             if (left == true)
                 Flip();
         }
 
         if (Vector3.Distance(target.position, transform.position) < 20)
         {
             transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
         }
         else
         {
             transform.Translate(Vector2.right * speed * Time.deltaTime);
         }
     }
 }


I also added a chase character function in the script above for fun. The problem is the punching bag is always getting punched towards the right. The animation works smoothly which is weird because the animation relies on which way the payer is to the punching bag. Can somebody please help?

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 TheElectroResistance · May 24, 2020 at 07:39 AM

Have you tried changing the direction of the force. @Alphacreations

 if (LookAtCharacter.left == false)
  {
          rb.AddForce(new Vector2(-150, 0));
  }
          else if (LookAtCharacter.right == false)
  {
          rb.AddForce(new Vector2(150, 0));
  }
Comment
Add comment · Show 1 · 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 Alphacreations · May 25, 2020 at 12:16 AM 0
Share

@TheElectroResistance That doesn't work, it just makes the punching bag always go left now

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

126 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

Related Questions

Knockback in 2d shooter 1 Answer

rb.AddForce-transform.right.. looks like teleporting how to fix?? 1 Answer

Knock Back only moving player up and down (vertical Y axis) 1 Answer

Trying to Lerp knock-back on Character Controller 1 Answer

How I can do knockback without OnColisson or OnTrigger 2 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