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 nsfnotthrowingaway · Jul 13, 2017 at 07:07 PM · collisionphysicsrigidbody2dcollider2doncollisionenter

Location of RigidBody2D on collision

How do I find the exact location of an object on collision. The contacts shows you the point where the two objects touched, but at that same moment of collision I need the position of the colliding object instead, not the point where they touched.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Jul 13, 2017 at 07:35 PM

The collision data object also stores a reference to the other collider. So you should be able to do something like collision.collider.transform.position.

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
avatar image
0

Answer by Mercbaker · Jul 13, 2017 at 07:43 PM

Anything stopping you from doing this?

   private void OnTriggerEnter2D(Collider2D col) {
         Debug.Log(col.gameObject.transform.position);
     }

This will return the objects position on TriggerEnter. You can do OnCollisionEnter as well.

Comment
Add comment · Show 3 · 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 nsfnotthrowingaway · Jul 13, 2017 at 10:04 PM 0
Share

The problem is that the object is a bouncing ball. When the ball bounces while moving semi-quickly, the position OnCollisionEnter is away from the wall before touching. OnCollisionExit it's away from the wall after touching. I think the physics is doing multiple iterations, and if the moment it touches a wall doesn't coincide with an update() then I the collision enter and exit are from the positions just before and just after the collision.

avatar image Mercbaker nsfnotthrowingaway · Jul 13, 2017 at 11:02 PM 0
Share

You may have to show an image or rephrase your question because:

  • col.gameObject.transform.position

  • transform.position

both of the above return the position of each respective object. (Which is what you asked for)

It does not return the point of the collision.

avatar image nsfnotthrowingaway Mercbaker · Jul 14, 2017 at 04:25 AM 0
Share

As an aside you can see that a Trail Renderer attached to the ball also skips the moment of impact and jumps from before the bounce to after in a straight line across rather than against the wall because there is no update() frame where the ball is actually touching the wall.

Also, if it matters I'm using continuous collision as discrete collision had the ball constantly going through walls.

full size image Alt text

avatar image
0

Answer by Mercbaker · Jul 14, 2017 at 07:02 AM

Okay, so I've achieved this result with the following setup.

  • Ball GameObject with a "knob" Sprite

  • CircleCollider2D and RidgidBody2D(continous), Trail Renderer

Attach "FireBall" script with following code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FireBall : MonoBehaviour {
 
     Rigidbody2D rb;
     [SerializeField]
     GameObject hitPoint;
 
     // Use this for initialization
     void Start() {
         rb = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update() {
 
         if (Input.GetKey("d")) {
             rb.AddForce(new Vector2(100, 200));
         }
         if (Input.GetKey("a")) {
             rb.AddForce(new Vector2(-100, 200));
         }
 
     }
 
     private void OnCollisionEnter2D(Collision2D col) {
         if (col.gameObject.tag == "wall") {
           
             GameObject obj = Instantiate(hitPoint, transform.position, Quaternion.identity);
      
         }
     }
 }
 

  • GameObject with BoxCollider2D and tag of "wall" > stretched to enclose the ball

  • Prefab serialized into the "FireBall" script as variable "hitPoint"


With the above setup you can achieve this result.

alt text

The ball leaves a copy of the prefab at the point where you have noted as your point of interest.

If this has helped, feel free to accept as the answer ;)


ballcorrectcollision.jpg (133.2 kB)
Comment
Add comment · Show 3 · 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 nsfnotthrowingaway · Jul 14, 2017 at 08:05 AM 0
Share

I appreciate the effort, but I'm sorry, that doesn't solve my problem. $$anonymous$$aybe the physics settings or ball speed is to blame for our different results, but using that code I still have the same issues with Trail Renderer and the ball position at bounce.

avatar image Mercbaker nsfnotthrowingaway · Jul 14, 2017 at 08:18 AM 0
Share

You should post how your moving the Ball in that case.

avatar image nsfnotthrowingaway Mercbaker · Jul 14, 2017 at 09:40 AM 0
Share

Using your code, if I hit 'd' 5 times that's enough to get it to happen. Physics2D gravity is set to -100, but it happens at regular gravity, too, just not as severe.

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

136 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

Related Questions

OnCollisionEnter inside FixedUpdate? 1 Answer

How do I get collisions between Tilemap Collider 2d and a Kinematic Rigidbody 2d? 1 Answer

Reset Rigidbody2D rotation after collision 2 Answers

OnCollisionStay2D is ignoring OnCollisionEnter2D 2 Answers

Changing transform.localScale but OnCollisionEnter isn't executed 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