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);
}
}
}
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);
}
}
}