other | この衝突に関係のある Collision2D データ |
オブジェクトのコライダーが別のコライダーに衝突したときに呼び出されます(2D 物理挙動のみ)
Further information about the collision is reported in the Collision2D parameter passed during the call. If you don't need this information then you can declare OnCollisionEnter2D without the parameter.
Note: Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.
関連項目: Collision2D クラス、OnCollisionExit2D、OnCollisionStay2D.
The folllowing two script examples create an OnCollisionEnter2D demo. Example1 generates
a (white) box
sprite called GameObject1
. This sprite collides with the Example2
GameObject2
which is the floor
sprite. The box
sprite can be moved using the up, down,
left and right keys. For example, once the box
has fallen to the floor
it can be pushed
upwards with the up key. Once the up key is released the box
will fall back to the floor
.
Each time the box
hits the floor
an OnCollisionEnter2D call will be made.
GameObject1
simply provides a string in the console to indicate the collision has
happened.
using UnityEngine;
// Create a box sprite which falls and hits a floor sprite. The box can be moved/animated // with the up, left, right, and down keys. Moving the box sprite upwards and letting it // fall will increase the number of calls from OnCollisionEnter2D.
public class Example1 : MonoBehaviour { public Texture2D tex;
void Awake() { SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer; transform.position = new Vector3(0.0f, 2.5f, 0.0f);
Sprite sp = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f); sr.sprite = sp;
gameObject.AddComponent<BoxCollider2D>();
Rigidbody2D rb = gameObject.AddComponent<Rigidbody2D>(); rb.bodyType = RigidbodyType2D.Dynamic; }
void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical");
gameObject.transform.Translate(moveHorizontal * 0.05f, moveVertical * 0.25f, 0.0f); }
// called when the cube hits the floor void OnCollisionEnter2D(Collision2D col) { Debug.Log("OnCollisionEnter2D"); } }
Example2. This creates the floor.
using UnityEngine;
public class Example2 : MonoBehaviour { public Texture2D tex;
void Awake() { SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer; transform.position = new Vector3(0.0f, -2.0f, 0.0f);
Sprite sp = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f); sr.sprite = sp;
gameObject.AddComponent<BoxCollider2D>(); } }
The two objects are stored as GameObjects each with one of the scripts. These GameObjects start with just the script example that it needs. Example1 is applied to GameObject1 and Example2 to GameObject2.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.