Collision and triggering in the 2D world
Collision conditions: Both objects must have collision components, and one object requires a rigid body component.
Trigger component: Both objects must have a collision component, and one of the colliders must check whether to trigger this option.
2d characters
public class Player : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
}
private void OnTriggerExit2D(Collider2D collision)
{
}
private void OnTriggerStay2D(Collider2D collision)
{
}
private void OnCollisionStay2D(Collision2D collision)
{
}
private void OnCollisionExit2D(Collision2D collision)
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
}
}
GroundGround
2D physical material, friction and rebound force
Physical materials added to the ground
2D physics effects
Surface effect, slides faster
Area effect, floating after entering the area
buoyancy effect
Constant force components
public class MouseInput : MonoBehaviour
{
Rigidbody2D r2;
void Start()
{
r2 = transform.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
if(x!=0 || y != 0)
{
r2.velocity = new Vector2(100 * x, 100 * y);
}
}
}