目次
前回のチュートリアル
スクリプトの更新
スクリプトファイルを開いて下記のコードを貼り付けて保存します
Controller2D.cs
using UnityEngine; using System.Collections; public class Controller2D : RaycastController { float maxClimbAngle = 80; float maxDescendAngle = 80; public CollisionInfo collisions; public override void Start() { base.Start (); collisions.faceDir = 1; } public void Move(Vector3 velocity, bool standingOnPlatform = false) { UpdateRaycastOrigins (); collisions.Reset (); collisions.velocityOld = velocity; if (velocity.x != 0) { collisions.faceDir = (int)Mathf.Sign(velocity.x); } if (velocity.y < 0) { DescendSlope(ref velocity); } HorizontalCollisions (ref velocity); if (velocity.y != 0) { VerticalCollisions (ref velocity); } transform.Translate (velocity); if (standingOnPlatform) { collisions.below = true; } } void HorizontalCollisions(ref Vector3 velocity) { float directionX = collisions.faceDir; float rayLength = Mathf.Abs (velocity.x) + skinWidth; if (Mathf.Abs(velocity.x) < skinWidth) { rayLength = 2*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) { if (hit.distance == 0) { continue; } float slopeAngle = Vector2.Angle(hit.normal, Vector2.up); if (i == 0 && slopeAngle <= maxClimbAngle) { if (collisions.descendingSlope) { collisions.descendingSlope = false; velocity = collisions.velocityOld; } float distanceToSlopeStart = 0; if (slopeAngle != collisions.slopeAngleOld) { distanceToSlopeStart = hit.distance-skinWidth; velocity.x -= distanceToSlopeStart * directionX; } ClimbSlope(ref velocity, slopeAngle); velocity.x += distanceToSlopeStart * directionX; } if (!collisions.climbingSlope || slopeAngle > maxClimbAngle) { velocity.x = (hit.distance - skinWidth) * directionX; rayLength = hit.distance; if (collisions.climbingSlope) { velocity.y = Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x); } collisions.left = directionX == -1; collisions.right = directionX == 1; } } } } 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; if (collisions.climbingSlope) { velocity.x = velocity.y / Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Sign(velocity.x); } collisions.below = directionY == -1; collisions.above = directionY == 1; } } if (collisions.climbingSlope) { float directionX = Mathf.Sign(velocity.x); rayLength = Mathf.Abs(velocity.x) + skinWidth; Vector2 rayOrigin = ((directionX == -1)?raycastOrigins.bottomLeft:raycastOrigins.bottomRight) + Vector2.up * velocity.y; RaycastHit2D hit = Physics2D.Raycast(rayOrigin,Vector2.right * directionX,rayLength,collisionMask); if (hit) { float slopeAngle = Vector2.Angle(hit.normal,Vector2.up); if (slopeAngle != collisions.slopeAngle) { velocity.x = (hit.distance - skinWidth) * directionX; collisions.slopeAngle = slopeAngle; } } } } void ClimbSlope(ref Vector3 velocity, float slopeAngle) { float moveDistance = Mathf.Abs (velocity.x); float climbVelocityY = Mathf.Sin (slopeAngle * Mathf.Deg2Rad) * moveDistance; if (velocity.y <= climbVelocityY) { velocity.y = climbVelocityY; velocity.x = Mathf.Cos (slopeAngle * Mathf.Deg2Rad) * moveDistance * Mathf.Sign (velocity.x); collisions.below = true; collisions.climbingSlope = true; collisions.slopeAngle = slopeAngle; } } void DescendSlope(ref Vector3 velocity) { float directionX = Mathf.Sign (velocity.x); Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomRight : raycastOrigins.bottomLeft; RaycastHit2D hit = Physics2D.Raycast (rayOrigin, -Vector2.up, Mathf.Infinity, collisionMask); if (hit) { float slopeAngle = Vector2.Angle(hit.normal, Vector2.up); if (slopeAngle != 0 && slopeAngle <= maxDescendAngle) { if (Mathf.Sign(hit.normal.x) == directionX) { if (hit.distance - skinWidth <= Mathf.Tan(slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x)) { float moveDistance = Mathf.Abs(velocity.x); float descendVelocityY = Mathf.Sin (slopeAngle * Mathf.Deg2Rad) * moveDistance; velocity.x = Mathf.Cos (slopeAngle * Mathf.Deg2Rad) * moveDistance * Mathf.Sign (velocity.x); velocity.y -= descendVelocityY; collisions.slopeAngle = slopeAngle; collisions.descendingSlope = true; collisions.below = true; } } } } } public struct CollisionInfo { public bool above, below; public bool left, right; public bool climbingSlope; public bool descendingSlope; public float slopeAngle, slopeAngleOld; public Vector3 velocityOld; public int faceDir; public void Reset() { above = below = false; left = right = false; climbingSlope = false; descendingSlope = false; slopeAngleOld = slopeAngle; slopeAngle = 0; } } }
Player.cs
using UnityEngine; using System.Collections; [RequireComponent (typeof (Controller2D))] public class Player : MonoBehaviour { public float jumpHeight = 4; public float timeToJumpApex = .4f; float accelerationTimeAirborne = .2f; float accelerationTimeGrounded = .1f; float moveSpeed = 6; public Vector2 wallJumpClimb; public Vector2 wallJumpOff; public Vector2 wallLeap; public float wallSlideSpeedMax = 3; public float wallStickTime = .25f; float timeToWallUnstick; float gravity; float jumpVelocity; Vector3 velocity; float velocityXSmoothing; Controller2D controller; void Start() { controller = GetComponent<Controller2D> (); gravity = -(2 * jumpHeight) / Mathf.Pow (timeToJumpApex, 2); jumpVelocity = Mathf.Abs(gravity) * timeToJumpApex; print ("Gravity: " + gravity + " Jump Velocity: " + jumpVelocity); } void Update() { Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical")); int wallDirX = (controller.collisions.left) ? -1 : 1; float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp (velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne); bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } if (Input.GetKeyDown (KeyCode.Space)) { if (wallSliding) { if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } if (controller.collisions.below) { velocity.y = jumpVelocity; } } velocity.y += gravity * Time.deltaTime; controller.Move (velocity * Time.deltaTime); } }
壁の配置
「Quad (1)」を選択して Delete を押して削除します
「Quad」を選択した状態で Ctrl + D を 2回押して
「Quad」を2つ複製します
そして、「Quad (1)」の「Position」「Scale」を上記のように設定します
次、「Quad (2)」の「Position」「Scale」を上記のように設定します
最後に、「Player」を選択して
これらのパラメータに上記のように値を設定します
これでゲームを再生して、壁に張り付きながらスペースキーを押すと
壁キックできることが確認できます