Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 christopherlegg1 · Aug 13, 2020 at 03:37 AM · collision2dknockback

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

Anyone know why my character will only move up and down (vertically) on collision? It won't move left and right.

Code:

      if (other.gameObject.CompareTag("Player"))
      {
         Rigidbody2D hit = other.GetComponent<Rigidbody2D>();
        
         if (hit != null)
         {
             Vector2 difference = hit.transform.position - this.transform.position;
             difference = difference.normalized * thrust;
             hit.AddForce(difference, ForceMode2D.Impulse);

             if (other.gameObject.CompareTag("Player") && other.isTrigger)
             {
                 other.GetComponent<PlayerMovement>().currentState = PlayerState.stagger;
                 other.GetComponent<PlayerMovement>().Knock(hit, knockTime);
                 if(source != null)
                 {
                     source = hit.GetComponent<AudioSource>();
                     source.Play();
                 }
             }
        }
   }

Other Code (from PlayerMovement referenced above):

 public void Knock(Rigidbody2D myRigidbody, float knockTime)
 {
     StartCoroutine(KnockCo(myRigidbody, knockTime));
 }
 private IEnumerator KnockCo(Rigidbody2D myRigidbody, float knockTime)
 {
     if (myRigidbody != null)
     {
         yield return new WaitForSeconds(knockTime);
         myRigidbody.velocity = Vector2.zero;
         currentState = PlayerState.idle;
     }
 }


Comment
Add comment · Show 1
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 CardboardComputers · Aug 13, 2020 at 05:05 AM 1
Share

Eek, obfuscated JS vibes

Why are you using the difference in position as the direction vector? Shouldn't it be based on velocity, or is there something specific you're going for? Is it a raycast or a collision? Are your Rigidbody2Ds set to kinematic or not? What's the setup look like, and do you have any more specific information on the behaviour? Does the bug persist when your character gets hit in midair?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Captain_Pineapple · Aug 13, 2020 at 09:13 AM

as @CardboardComputers pointed out it is difficult/not possible to tell what's going on from the currntly provided information.


However my guess would be that you (just as too many before you) are doing one of the following:

  • directly manipulating the characters horizontal velocity to move the character

  • moving the character by moveposition or movetowards


Both versions would be a problem here since you overwrite the effects of "addForce" each frame.


if that is the case you either need to change the way you move your character for a force based system or implement your own knockback "force" that does not use rigidbody.addforce.

Comment
Add comment · Show 6 · 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 christopherlegg1 · Aug 13, 2020 at 10:07 AM 0
Share

I tried disabling the controls completely and it still only pushes back up or down (when the enemy touches a player). I made an alternate code that gives a huge addforce and when I do, it seems to push the X value away 1 time, and the Y value away for the time specified by the timer.

(This is a top down game, not a platformer.) The controls move the character in all 4 directions but only the horizontal doesnt get affected when he gets hit.

What information would be helpful to submit to more accurately understand my problem do you think?

avatar image Captain_Pineapple christopherlegg1 · Aug 13, 2020 at 11:12 AM 0
Share

The code you use to move your character would be helpful. Your experiment with "trying to disable the movement controls" only further amplifies my assumption that you overwrite the objects x velocity somewhere.

avatar image christopherlegg1 Captain_Pineapple · Aug 14, 2020 at 10:30 AM 0
Share
 // Start is called before the first frame update
 void Start()
 {
    if(startingPosition != null) transform.position = startingPosition.initialValue;
     currentState = PlayerState.walk;
     animator = GetComponent<Animator>();
     myRigidbody = GetComponent<Rigidbody2D>();
     animator.SetFloat("moveX", 0);
     animator.SetFloat("moveY", -1);

     player$$anonymous$$axHp = GameObject.Find("playerstats").GetComponent<playerStats>().player$$anonymous$$axHp;
     playerHp = GameObject.Find("playerstats").GetComponent<playerStats>().playerHp;
     expToNextLevel = GameObject.Find("playerstats").GetComponent<playerStats>().expToNextLevel;
     exp = GameObject.Find("playerstats").GetComponent<playerStats>().exp;
     playerLevel = GameObject.Find("playerstats").GetComponent<playerStats>().playerLevel;
     chests = 0;
 }
   
 void FixedUpdate()
 {
     //$$anonymous$$y Code to stop sliding
     if (currentState == PlayerState.idle || currentState == PlayerState.walk)
     {
        myRigidbody.velocity = Vector2.zero;
     }
    
     change = Vector3.zero;
     change.x = Input.GetAxisRaw("Horizontal");
     change.y = Input.GetAxisRaw("Vertical");
     UpdateAnimationAnd$$anonymous$$ove();

 }
 void Update()
 {
     if (Input.GetButtonDown("attack") && currentState != PlayerState.attack && currentState != PlayerState.stagger)
     {
         StartCoroutine(AttackCo());
     }
     else if (currentState == PlayerState.walk || currentState == PlayerState.idle)
     {
         UpdateAnimationAnd$$anonymous$$ove();
     }
 }


private IEnumerator AttackCo() { animator.SetBool("attacking", true); currentState = PlayerState.attack; yield return null; animator.SetBool("attacking", false); yield return new WaitForSeconds(0.16f); currentState = PlayerState.walk; GameObject.Find("playerstats").GetComponent().superBarCurrent += 5; }

 void UpdateAnimationAnd$$anonymous$$ove()
 {
     if (change != Vector3.zero)
     {
         $$anonymous$$oveCharacter();
         animator.SetFloat("moveX", change.x);
         animator.SetFloat("moveY", change.y);
         animator.SetBool("moving", true);
     }
     else
     {
         animator.SetBool("moving", false);
     }
 }
 //$$anonymous$$ovement
 void $$anonymous$$oveCharacter()
 {
     if (currentState != PlayerState.attack)
     {
     change.Normalize();
     myRigidbody.$$anonymous$$ovePosition(transform.position + change * speed * Time.deltaTime);
     }
 }

 public void Knock(Rigidbody2D myRigidbody, float knockTime)
 {
     StartCoroutine(KnockCo(myRigidbody, knockTime));
 }
 private IEnumerator KnockCo(Rigidbody2D myRigidbody, float knockTime)
 {
     if (myRigidbody != null)
     {
         yield return new WaitForSeconds(knockTime);
         myRigidbody.velocity = Vector2.zero;
         currentState = PlayerState.idle;
         myRigidbody.velocity = Vector2.zero;

     }
 }

}

Show more comments

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

134 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

Related Questions

How do I effectively perform KnockBack whenever my character gets hit by an enemy/ harmful object? 0 Answers

Allow collision but take no force from it, not increasing object mass 1 Answer

[Unity 2D] Knockback in a 2D platformer 1 Answer

Tricky issue involving knockback and the player. 1 Answer

How can I add knockback to a prefab? 1 Answer


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