- Home /
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.
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.
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.
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.
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.
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.
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.
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 ;)
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.
You should post how your moving the Ball in that case.
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
Follow this Question
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