Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- [RequireComponent (typeof (BoxCollider2D))]
- public class RaycastController : MonoBehaviour {
- public LayerMask collisionMask;
- public const float skinWidth = .015f;
- public int horizontalRayCount = 4;
- public int verticalRayCount = 4;
- [HideInInspector]
- public float horizontalRaySpacing;
- [HideInInspector]
- public float verticalRaySpacing;
- [HideInInspector]
- public BoxCollider2D collider;
- public RaycastOrigins raycastOrigins;
- public virtual void Awake() {
- collider = GetComponent<BoxCollider2D> ();
- }
- public virtual void Start() {
- CalculateRaySpacing ();
- }
- public 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);
- }
- public 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);
- }
- public struct RaycastOrigins {
- public Vector2 topLeft, topRight;
- public Vector2 bottomLeft, bottomRight;
- }
- }
- 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;
- 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() {
- if (controller.collisions.above || controller.collisions.below) {
- velocity.y = 0;
- }
- Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
- if (Input.GetKeyDown (KeyCode.Space) && controller.collisions.below) {
- velocity.y = jumpVelocity;
- }
- float targetVelocityX = input.x * moveSpeed;
- velocity.x = Mathf.SmoothDamp (velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
- velocity.y += gravity * Time.deltaTime;
- controller.Move (velocity * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement