- Home /
The question is answered, right answer was accepted
Debug a rigidbody2d velocity on an exact point
Hi! I'm trying to debug the velocity just on the impact on a collider(Wall) coming from a Rigidbody2D This object is a square and there is no rotation on it only movement on the X and Y axis I made this simple script but it doesn't give me any feedback on the Console Any idea why? (I can already feel how easy is the solution and wrong the script is T-T)
using UnityEngine;
using System.Collections;
public class InfoHolder : MonoBehaviour {
public GameObject ball;
void OnCollisionEnter(Collision collision){
if(collision.gameObject == ball){
Debug.Log (ball.GetComponent<Rigidbody2D>().velocity);
}
}
}
Answer by MelvMay · Apr 13, 2016 at 07:22 AM
For starters, you're using the 3D collision callback 'OnCollisionEnter'. You need to use the 2D callback 'OnCollisionEnter2D' if you're using 2D physics. Note this uses 'Collision2D'.
Also, I can only presume you've assigned something to the 'ball' field otherwise it'll always be NULL which you'll never get.
Finally, you could also make it much simpler by having a field of Rigidbody2D which is passed into the callback so you don't have to search for a component which is just wasting precious cycles like so:
using UnityEngine;
using System.Collections;
public class InfoHolder : MonoBehaviour {
public Rigidbody2D ball;
void OnCollisionEnter(Collision collision){
if(collision.rigidbody == ball){
Debug.Log (rigidbody.velocity);
}
}
}
Very good answer $$anonymous$$elv! But even better was how you give me the answer! (y) A lot of people around here have turn to be real idiots when they talk with Noobs like me! Thanks! =)
Np, we've all got to start somewhere. :)
Good luck.