반응형
Java의 Reflection과 유사.
String으로 매소드 이름과 매개변수를 보냄.
MMPreventPassingThrough3D.cs (Sample)
/// <summary>
/// On fixedUpdate, checks the last movement and if needed casts a ray to detect obstacles
/// </summary>
protected virtual void FixedUpdate()
{
_lastMovement = this.transform.position - _positionLastFrame;
_lastMovementSquared = _lastMovement.sqrMagnitude;
// if we've moved further than our bounds, we may have missed something
if (_lastMovementSquared > _squaredBoundsWidth)
{
float movementMagnitude = Mathf.Sqrt(_lastMovementSquared);
// we cast a ray backwards to see if we should have hit something
RaycastHit hitInfo;
if (Physics.Raycast(_positionLastFrame, _lastMovement, out hitInfo, movementMagnitude, ObstaclesLayerMask.value))
{
if (!hitInfo.collider)
{
return;
}
if (hitInfo.collider.isTrigger)
{
hitInfo.collider.SendMessage("OnTriggerEnter", _collider);
}
if (!hitInfo.collider.isTrigger)
{
this.gameObject.SendMessage("PreventedCollision3D", hitInfo, SendMessageOptions.DontRequireReceiver);
if (RepositionRigidbody)
{
var hitLayer = hitInfo.collider.gameObject.layer;
if (0 != (1 << hitLayer & RepositionRigidbodyLayerMask))
{
this.transform.position = hitInfo.point - (_lastMovement / movementMagnitude) * _adjustedSmallestBoundsWidth;
_rigidbody.position = hitInfo.point - (_lastMovement / movementMagnitude) * _adjustedSmallestBoundsWidth;
}
}
}
}
}
_positionLastFrame = this.transform.position;
}
BouncyProjectile.cs
/// <summary>
/// If we get a prevent collision 3D message, we check if we should bounce
/// </summary>
/// <param name="hit"></param>
public void PreventedCollision3D(RaycastHit hit)
{
_raycastDirection = transform.position - _positionLastFrame;
if (_health.CurrentHealth <= 0)
{
return;
}
EvaluateHit3D(hit);
}
반응형
'Unity > Unity 리서치' 카테고리의 다른 글
Unity 포물선 발사체 (0) | 2023.03.04 |
---|---|
C# 확장매소드(Extension Method) (0) | 2023.02.16 |
Enum을 활용한 Bit연산자 Flag (0) | 2023.01.24 |
Collider2D, Collider 상속 관계 (0) | 2023.01.23 |
유니티 강좌 모음 (0) | 2023.01.03 |