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 unity_szuBm7Q_m1sWyw · Nov 18, 2018 at 10:34 PM · movementphysics2drigidbody.velocitybouncingphysics2d.raycast

Odd behavior when circle collider is supposed to bounce off wall

Hello. First time posting here. So I have a script written for when a 2D circle collider representing an object is in motion and hits a solid object. In my game the surfaces are only along the X and Y axis since they are boxes. I use 4 raycasts from the center of the object which extend the radius of the ball going up, down, left, and right to detect object collision, since the collider is a trigger. On my trigger enter, these 4 raycasts are sent and if one of them collides, I am able to determine which side of the ball was collided with. If it's the top or bottom, I reverse the Y velocity, and if it's the sides, I reverse the X velocity. This is to simulate a linear bounce off a wall of the ball.

The issue is that when the object collides with a wall, it only seems to work properly if it's travelling a specific direction and hits a specific side. For instance, if the ball is sent at a speed to the right and hits a wall, it will pass right through. However, from the left it will bounce as intended. Is there an error in the way I've written this code that explains this odd behavior? Please examine my code:

 public class sword : MonoBehaviour {
 
 
     public Rigidbody2D rb;
 
     private float spin, ball_radius;
     public float speed, trajectory;
     public GameObject Player;
     Animator anim;
 
     // Use this for initialization
     void Start () {
         speed = 0.0f;
         anim = Player.GetComponent<Animator>();
         trajectory = 0.0f;
         ball_radius = 0.15f;
     }
 
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
 
 
         if (collision.tag == "player" && speed <= 1)
         {
             transform.position = new Vector2(-1000, -1000);
             anim.SetBool("Sword", true);
             Player.GetComponent<movement>().equipped = true;
         }
         if (collision.tag == "solid")
         {
             RaycastHit2D hitup = Physics2D.Raycast(transform.position, Vector2.up, ball_radius);
             RaycastHit2D hitright = Physics2D.Raycast(transform.position, Vector2.right, ball_radius);
             RaycastHit2D hitdown = Physics2D.Raycast(transform.position, Vector2.down, ball_radius);
             RaycastHit2D hitleft = Physics2D.Raycast(transform.position, Vector2.left, ball_radius);
 
             if (hitup.collider != null)
             {
                 trajectory = Mathf.Rad2Deg * (Mathf.Atan(-rb.velocity[1] / rb.velocity[0]));
             }
             if (hitright.collider != null)
             {
                 trajectory = Mathf.Rad2Deg * (Mathf.Atan(rb.velocity[1] / -rb.velocity[0]));
             }
             if (hitdown.collider != null)
             {
                 trajectory = Mathf.Rad2Deg * (Mathf.Atan(-rb.velocity[1] / rb.velocity[0]));
             }
             if (hitleft.collider != null)
             {
                 trajectory = Mathf.Rad2Deg * (Mathf.Atan(rb.velocity[1] / -rb.velocity[0]));
             }
 
         }
     }
 
     private void FixedUpdate()
     {
         rb.velocity = new Vector2(speed * Mathf.Cos(trajectory * Mathf.Deg2Rad), speed * Mathf.Sin(trajectory * Mathf.Deg2Rad));
         speed = speed * 0.93f;
     }
 }
 

Note that I have to convert degrees to radians in the final movement calculation because the way my object it moved is based on the Euler rotation of another object. A different script handles setting the objects speed when it travels, in case you are wondering how it begins acceleration in the first place. Thanks for looking at this problem for me.

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
1

Answer by Cornelis-de-Jager · Nov 19, 2018 at 01:55 AM

Hi, I assumed it is something to do with how you calculate your trajectory. You are using the same trajectory with for both hitting Up and Down and then again the same for Left and Right. I assume the aTan calculation is returning the same result for both positive and negative values. Might need to test that. however I have made a different solution below without using the math and raycasts:

 Vector2 trajectory; // Change this to vectory 2
 
 private void OnTriggerEnter2D(Collider2D collision)
 { 
     Vector2 vec = collision.point - transform.position;
     var x = Math.Abs(vec.x);
     var y = Math.Abs(vec.y);
 
     if (x > y) {
         // Change x direciton
         trajectory= new Vector2 (-rb.velocity[0], rb.velocity[1]);
     } eles if (y > x){
         // Change y direciton
         trajectory = new Vector2 (rb.velocity[0], -rb.velocity[1]);
     } else {
         // Change both
         trajectory = new Vector2 (-rb.velocity[0], -rb.velocity[1]);
     }
 }
 
 
 private void FixedUpdate()
 {
     rb.velocity = trajectory * speed;
     speed = speed * 0.93f;
 }
Comment
Add comment · Show 2 · 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 unity_szuBm7Q_m1sWyw · Nov 19, 2018 at 04:55 AM 0
Share

Thanks for your answer! The code works much better now, however there is still odd behavior happening typically when I try to bounce it at more flat angles. It sometimes bounces the wrong way or phases through the object. I've had to adjust your code a bit. For one, when you set rb.velocity[0] or [1] to be negative or positive, I believe you meant to set trajectory[0] and [1], since doing the velocity causes a very large speedup. I also had to use a raycast in the end, since I am using a trigger and therefore do not have a point of contact. I think the issue has to do with the raycast. Can you suggest an alternative method for finding the contact point if I'm using a trigger? The reason it is a trigger is because I was finding it difficult to remove the unity engine physics I needed to remove from a regular circle collider, like when the ball collided with a non solid object for example. Here is my edited code:

     private void OnTriggerEnter2D(Collider2D collision)
     {
 
 
         if (collision.tag == "player" && speed <= 1)
         {
             transform.position = new Vector2(-1000, -1000);
             anim.SetBool("Sword", true);
             Player.GetComponent<movement>().equipped = true;
         }
         if (collision.tag == "solid")
         {
             Debug.Log(trajectory);
             hit = Physics2D.CircleCast(transform.position, ball_radius, trajectory);
             var x = $$anonymous$$athf.Abs(hit.point[0]);
             var y = $$anonymous$$athf.Abs(hit.point[1]);
 
             if (x > y)
             {
                 // Change x direciton
                 trajectory = new Vector2(-trajectory[0], trajectory[1]);
             }
             else if (y > x)
             {
                 // Change y direciton
                 trajectory = new Vector2(trajectory[0], -trajectory[1]);
             }
             else
             {
                 // Change both
                 trajectory = new Vector2(-trajectory[0], -trajectory[1]);
             }
 
         }
     }
 
     private void FixedUpdate()
     {
 
         rb.velocity = speed * trajectory;
         speed = speed * 0.93f;
     }
avatar image unity_szuBm7Q_m1sWyw · Nov 20, 2018 at 02:15 AM 0
Share

Hi again. Hope you are still around. I've simplidied the code even more using Unity methods, and now it works about the same as your original answer. Still having those odd issues, can you identify an issue with this code?

 public class sword : $$anonymous$$onoBehaviour {
 
     public Rigidbody2D rb;
     public CircleCollider2D coll;
 
     private float spin;
     public float speed;
     public Vector2 trajectory;
     public GameObject Player;
     Animator anim;
     RaycastHit2D hit;
 
     void Start () {
         speed = 0.0f;
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
 
         if (collision.tag == "solid")
         {
             hit = Physics2D.Raycast(transform.position, trajectory);
             trajectory = Vector2.Reflect(trajectory, hit.normal);
 
         }
     }
 private void FixedUpdate()
 {

     rb.velocity = speed * trajectory;
     speed = speed * 0.93f;
 }
 }

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

152 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

Related Questions

Player can't move, Boxcast Problem 1 Answer

How can I add an impulse force on top of my walking force? 1 Answer

PhysicsMaterial2D and bounciness: Getting weird angles when hitting box collider edges 2 Answers

I do you control movements and apply other forces at the same time ? 0 Answers

HitStop problems 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