• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

What exactly is RelativeVelocity? 1 Answer

Workarounds for Higher Control When Using Physics2D? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges