Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 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 whamu · Apr 25 at 02:55 AM · 2d-platformerrigidbody2daddforceplatformerknockback

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

This piece of code attached to a bullet game object that is supposed to get the distance between the other player and bullet and move the other player in the opposite direction only works properly along the Y axis. Basically when I shoot another player it just causes them to jump up. This is not to say that it doesn't move at all at the x axis, as the x axis of the player did show small change in the inspector, and when I lowered the mass to 0.001 the bullets made the object teleport a little bit on the x axis.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Photon.Pun;
 public class bullet : MonoBehaviour
 {
     playercont victim;
     private float speed = 5;
     private Rigidbody2D rb;
     float life = 4;
 
      float dur = 5.5f;
     float push = 1.5f;
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         rb.velocity = transform.up * speed;
     }
     private void FixedUpdate()
     {
         life -= Time.deltaTime;
         if (life <= 0)
         {
             Destroy(gameObject);
         }
     }
     
            
     private void OnTriggerEnter2D(Collider2D other)
     {
         Rigidbody2D enem = other.GetComponent<Rigidbody2D>();
         if (enem!= null)
         {
             Vector2 dif = enem.transform.position - rb.transform.position;
             dif = dif.normalized * push;
             enem.AddForce(dif);
            StartCoroutine(knocktime(enem));
             Destroy(gameObject);
         }
    }
 
 void Update()
     {
         
     }
     private IEnumerator knocktime(Rigidbody2D enem)
     {
         if (enem != null)
         {
             yield return new WaitForSeconds(dur);
             enem.velocity = Vector2.zero;
            
         }
 
     }
 }

Keep in mind that while this is a platformer, I turned of gravity to make sure it's not friction, if it was working properly it would just float away, which it didn't.

,This piece of code attached to a bullet game object that is supposed to get the distance between the other player and bullet and move the other player in the opposite direction only works properly along the Y axis. Basically when I shoot another player it just causes them to jump up. This is not to say that it doesn't move at all at the x axis, as the x axis of the player did show small change in the inspector, and when I lowered the mass to 0.001 the bullets made the object teleport a little bit on the x axis.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Photon.Pun;
 public class bullet : MonoBehaviour
 {
     playercont victim;
     private float speed = 5;
     private Rigidbody2D rb;
     float life = 4;
 
      float dur = 5.5f;
     float push = 1.5f;
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         rb.velocity = transform.up * speed;
     }
     private void FixedUpdate()
     {
         life -= Time.deltaTime;
         if (life <= 0)
         {
             Destroy(gameObject);
         }
     }
     
            
     private void OnTriggerEnter2D(Collider2D other)
     {
         Rigidbody2D enem = other.GetComponent<Rigidbody2D>();
         if (enem!= null)
         {
             Vector2 dif = enem.transform.position - rb.transform.position;
             dif = dif.normalized * push;
             enem.AddForce(dif);
            StartCoroutine(knocktime(enem));
             Destroy(gameObject);
         }
    }
 
 void Update()
     {
         
     }
     private IEnumerator knocktime(Rigidbody2D enem)
     {
         if (enem != null)
         {
             yield return new WaitForSeconds(dur);
             enem.velocity = Vector2.zero;
            
         }
 
     }
 }

Keep in mind that while this is a platformer, I turned of gravity to make sure it's not friction, if it was working properly it would just float away, which it didn't.

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 whamu · Apr 26 at 01:02 AM

yh turns out it was because I was setting the rigidbody velocity on the player script.

Comment
Add comment · 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

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

158 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

Related Questions

Moving platform player bounce when moving down 1 Answer

AddForce to x axis when mouse position is close to x axis ? And if closer to x axis the more addforce 0 Answers

Unity 2D addforce problem 1 Answer

How do I get ForceMode2D.Impulse to work? 2 Answers

knockback freezing game 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