2016-04-03 1 views
-3

が含まれていませんUnityEngineを使用してC#のは、私のコードの定義なし拡張メソッド

。 using System.Collections;

[RequireComponent(typeof演算(BoxCollider2D))] パブリッククラスController2D:MonoBehaviour {

public LayerMask collisionMask; 

const float skinWidth = .015f; 
public int horizontalRayCount = 4; 
public int verticalRayCount = 4; 

float horizontalRaySpacing; 
float verticalRaySpacing; 

new BoxCollider2D collider; 
RaycastOrigins raycastOrigins; 
internal object collisions; 

void Start() 
{ 
    collider = GetComponent<BoxCollider2D>(); 
    CalculateRaySpacing(); 
} 

public void Move(Vector3 velocity) 
{ 
    UpdateRaycastOrigins(); 
    if (velocity.x != 0) 
    { 
     HorizontalCollisions(ref velocity); 
    } 
    if (velocity.y != 0) 
    { 
     VerticalCollisions(ref velocity); 
    } 

    transform.Translate(velocity); 
} 

void HorizontalCollisions(ref Vector3 velocity) 
{ 
    float directionX = Mathf.Sign(velocity.x); 
    float rayLength = Mathf.Abs(velocity.x) + skinWidth; 

    for (int i = 0; i < horizontalRayCount; i++) 
    { 
     Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight; 
     rayOrigin += Vector2.up * (horizontalRaySpacing * i); 
     RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask); 

     Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.red); 

     if (hit) 
     { 
      velocity.x = (hit.distance - skinWidth) * directionX; 
      rayLength = hit.distance; 
     } 
    } 
} 

void VerticalCollisions(ref Vector3 velocity) 
{ 
    float directionY = Mathf.Sign(velocity.y); 
    float rayLength = Mathf.Abs(velocity.y) + skinWidth; 

    for (int i = 0; i < verticalRayCount; i++) 
    { 
     Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft; 
     rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x); 
     RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask); 

     Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red); 

     if (hit) 
     { 
      velocity.y = (hit.distance - skinWidth) * directionY; 
      rayLength = hit.distance; 
     } 
    } 
} 

void UpdateRaycastOrigins() 
{ 
    Bounds bounds = collider.bounds; 
    bounds.Expand(skinWidth * -2); 

    raycastOrigins.bottomLeft = new Vector2(bounds.min.x, bounds.min.y); 
    raycastOrigins.bottomRight = new Vector2(bounds.max.x, bounds.min.y); 
    raycastOrigins.topLeft = new Vector2(bounds.min.x, bounds.max.y); 
    raycastOrigins.topRight = new Vector2(bounds.max.x, bounds.max.y); 
} 

void CalculateRaySpacing() 
{ 
    Bounds bounds = collider.bounds; 
    bounds.Expand(skinWidth * -2); 

    horizontalRayCount = Mathf.Clamp(horizontalRayCount, 2, int.MaxValue); 
    verticalRayCount = Mathf.Clamp(verticalRayCount, 2, int.MaxValue); 

    horizontalRaySpacing = bounds.size.y/(horizontalRayCount - 1); 
    verticalRaySpacing = bounds.size.x/(verticalRayCount - 1); 
} 

struct RaycastOrigins 
{ 
    public Vector2 topLeft, topRight; 
    public Vector2 bottomLeft, bottomRight; 
} 

}

Iが "上" にカーソルを合わせたとき、それは私にエラーを与える "下"

c#には、拡張方法の定義と拡張方法が含まれていません。

誰でも私が間違っていることを見ることができますか?

ありがとうございます!

+1

ここにエラーが表示されます。あなたはそれが定義を示さないものを示していませんでした。 – Nkosi

+0

"above"と "below"のすべてのエラーが表示されます –

+0

メッセージは、通常、そのクラスにメソッドが存在せず、そのクラスを対象とする参照拡張メソッドがない場合に発生します。 'controller'が実際に' collisions'プロパティを持っていることを確認してください。そして、これまでに型の衝突がプロパティ名 'above'または' below'を持っていたことを確認してください。 – Nkosi

答えて

1
internal object collisions; 

ここでは、オブジェクトとして衝突を定義します。つまり、ブール値のプロパティを上回ったり下回ったりしません。
おそらく、collisionsという名前の新しい構造体またはクラスを定義し、上と下の内部または公開のブール値のプロパティを定義する必要があります。

関連する問題