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 unity_szuBm7Q_m1sWyw · Nov 20, 2018 at 02:36 AM · physicsphysics2dvector2collision2dreflect

Is it possible to find the contact point of a circle collider 2D with another collider inside of OnTriggerEnter2D?

Hello. I'm at a roadblock in my code trying to find a way to make a moving circle collider reflect off the walls in a linear fashion, similar to pong. After much research and help, I've found a good way to reflect the collider, but not a perfect way to detect the collision point. This code functions well if the collision point is accurate, but often times it isn't. I believe this is due to an unpredicted result from my raycast. I was wondering if anyone had a suggestion that could remove these unexpected results?

Some notes: My speed gets set from an exterior script. The trajectory is an angle converted to a Vector2, it's definitely accurate as well.

 public class sword : MonoBehaviour {
 
     public Rigidbody2D rb;
     public CircleCollider2D coll;
 
     private float spin;
     public float speed;
     public Vector2 trajectory;
     public GameObject Player;
     Animator anim;
     RaycastHit2D hit;
 
 
 
 
     // Use this for initialization
     void Start () {
         speed = 0.0f;
     }
 
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
 
 
         if (collision.tag == "player" && speed <= 1)
         {
             transform.position = new Vector2(-1000, -1000);
             Player.GetComponent<movement>().equipped = true;
         }
         if (collision.tag == "solid")
         {
             hit = Physics2D.Raycast(transform.position, trajectory);
             trajectory = Vector2.Reflect(trajectory, hit.normal);
         }
     }
 
 
     private void FixedUpdate()
     {
         //constantly calculating new velocity
         rb.velocity = speed * trajectory;
         speed = speed * 0.93f; //drag
     }
 }
 
 
 
 
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 20, 2018 at 03:03 AM

To get the point of collision simply use:

 var point = collision.point;
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 20, 2018 at 03:10 AM 0
Share

Unfortunately that is not a value when using OnTriggerEnter2D.

avatar image unity_szuBm7Q_m1sWyw · Nov 20, 2018 at 03:38 AM 0
Share

I have done it now in a way that uses OnCollisionEnter2D, but for some reason the if statement for when the tag == "solid" never triggers, even though it did before.

 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;
 
 
 
 
     // Use this for initialization
     void Start () {
         speed = 0.0f;
         anim = Player.GetComponent<Animator>();
     }
 
 
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
 
         if (collision.gameObject.tag == "player")
         {
             transform.position = new Vector2(-1000, -1000);
             anim.SetBool("Sword", true);
             Player.GetComponent<movement>().equipped = true;
         }
         if (collision.gameObject.tag == "solid")
         {
             ContactPoint2D otherObject = collision.contacts[0];
             Debug.Log(collision);
 
             Vector2 hit = otherObject.point;
             Vector2 normal = otherObject.normal;
             trajectory = Vector2.Reflect(trajectory, normal);
 
         }
     }
 
     private void FixedUpdate()
     {
         //constantly calculating new velocity
         rb.velocity = speed * trajectory;
         speed = speed * 0.93f; //drag
         if (speed < 0.0001f)
         {
             speed = 0.0f;
         }
         if (speed > 1)
         {
             Physics2D.IgnoreCollision(Player.GetComponent<CircleCollider2D>(), coll, true);
         }
         else
         {
             Physics2D.IgnoreCollision(Player.GetComponent<CircleCollider2D>(), coll, false);
         }
     }
 }

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

163 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

Related Questions

How does Collision2D.relativeVelocity Work? 0 Answers

Shoot Projectile on the direction of where the gun is facing 1 Answer

How to calculate a RigidBody2D's bounce from a collider? 1 Answer

Trigger is not executed 1 Answer

Inexact rebound when colliding into corner of two walls. 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