Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Player : MonoBehaviour {
- public float moveSpeed = 20f;
- public float maxSpeed = 20f;
- public float friction = 1f;
- public Vector3 frict;
- public Transform[] debug;
- public float ts = 1f;
- [Range(0,100)]
- public float AirControl = 100f;
- [Range(0,100)]
- public float AirFriction = 100f;
- public float JumpHeight = 10f;
- public bool Grounded = false;
- private BoxCollider2D boxcc = null;
- private Vector3 lastpos = Vector3.zero;
- public Vector3 Velocity = Vector3.zero;
- private LineRenderer lr;
- private float haxis = 0f;
- private float SpeedScale = 100f; //Could have better name?
- public static Player Instance; //temp singleton
- public Vector3 feet = Vector3.zero;
- // Use this for initialization
- void Awake () {
- Instance = this;
- boxcc = GetComponent<BoxCollider2D>();
- lr = GetComponentInChildren<LineRenderer>();
- }
- void AddVelocity(Vector3 vel)
- {
- Velocity += vel / SpeedScale;
- }
- void AddVelocity(float x, float y, float z)
- {
- Velocity.x += x / SpeedScale;
- Velocity.y += y / SpeedScale;
- Velocity.z += z / SpeedScale;
- }
- Vector3 GetVelocity()
- {
- return Velocity * SpeedScale;
- }
- void Land(Vector3 r)
- {
- Vector3 ground = r;
- ground.y += boxcc.bounds.extents.y;
- Grounded = true;
- transform.Translate(0f, ground.y - transform.position.y, 0f);
- //AddVelocity (0f, ground.y - transform.position.y, 0f);
- Velocity.y = 0f;
- }
- RaycastHit2D ClosestRaycastAll(RaycastHit2D[] rays)
- {
- if (rays.Length == 0)
- return new RaycastHit2D();
- float distance = Mathf.Infinity;
- RaycastHit2D closest = new RaycastHit2D();
- foreach (RaycastHit2D r in rays)
- {
- if (r.collider != boxcc)
- {
- float sqrmg = ((Vector3) r.point - transform.position).sqrMagnitude;
- if (sqrmg < distance)
- {
- closest = r;
- distance = sqrmg;
- }
- }
- }
- return closest;
- }
- void FixedUpdate()
- {
- Vector2 lastposcheck = lastpos;
- lastposcheck.y += boxcc.bounds.extents.y;
- Vector2 poscheck = transform.position;
- poscheck.y -= boxcc.bounds.extents.y;
- feet = poscheck;
- float chkdistance = 0.8f;
- //Gravity
- if (!Grounded)
- AddVelocity (Physics2D.gravity * Time.deltaTime);
- //
- //If we're falling, check for ground
- //
- if (GetVelocity ().y < 0f && !Grounded)
- {
- RaycastHit2D[] ray = Physics2D.RaycastAll(poscheck, Velocity, Vector2.Distance (transform.position, transform.position + Velocity));
- RaycastHit2D closest = ClosestRaycastAll(ray);
- if (closest.collider != null)
- Land(closest.point);
- }
- //
- //If we aren't moving upwards, check that we are still on the ground
- //Also check for ground in a box below us
- //
- if (GetVelocity ().y <= 0f)
- {
- Vector2 poscheckbox = transform.position;
- //poscheckbox.y -= chkdistance / 2;
- Vector2 bxchkextents = boxcc.bounds.size;
- bxchkextents.y /= 1.1f;
- bxchkextents.x /= 1.1f;
- RaycastHit2D groundcheckray = Physics2D.BoxCast(poscheckbox, bxchkextents, 0f, -Vector2.up, chkdistance);
- if (groundcheckray.collider == null)
- {
- Grounded = false;
- }
- else
- {
- if ((poscheck.y + Velocity.y) > groundcheckray.point.y)
- {
- Grounded = false;
- }
- if (!Grounded && (poscheck.y + Velocity.y) <= groundcheckray.point.y)
- {
- Land (groundcheckray.point);
- //Debug
- Debug.Log ("Landed box");
- }
- }
- }
- //Horizontal movement
- Vector3 HorVel = Vector3.zero;
- if (GetVelocity ().x < (maxSpeed * haxis) && haxis > 0f)
- HorVel = ( Vector3.right * Mathf.Min ( haxis * moveSpeed * Time.deltaTime, (maxSpeed - GetVelocity ().x) * haxis));
- if (GetVelocity ().x > (maxSpeed * haxis) && haxis < 0f)
- HorVel = ( Vector3.right * Mathf.Max ( haxis * moveSpeed * Time.deltaTime, (-maxSpeed - GetVelocity ().x) * -haxis));
- if (!Grounded)
- HorVel *= AirControl / 100f;
- AddVelocity (HorVel);
- //Friction
- frict = Vector3.zero;
- float temphaxis = (haxis < 0f ? -haxis : haxis);
- temphaxis = (temphaxis / -1) + 1;
- if ((GetVelocity ().x > haxis * maxSpeed || GetVelocity().x < haxis * maxSpeed) && GetVelocity ().x != 0f)
- frict = Vector3.right * ((GetVelocity ().x > 0f ? Mathf.Max (-friction, -GetVelocity ().x) : Mathf.Min (friction, -GetVelocity().x) )) * temphaxis;
- if (!Grounded)
- frict *= AirFriction / 100f;
- AddVelocity (frict);
- //
- //Check horizontal collision
- //
- if (Velocity.x != 0f)
- {
- RaycastHit2D horray;
- Vector2 sideboxcheckbounds = boxcc.bounds.size;
- sideboxcheckbounds.y /= 1.1f;
- sideboxcheckbounds.x /= 1.1f;
- horray = Physics2D.BoxCast(transform.position, sideboxcheckbounds, 0f, Velocity.x > 0 ? Vector2.right : -Vector2.right, chkdistance);
- if (horray.collider != null)
- {
- if ((Velocity.x < 0f && (transform.position.x - boxcc.bounds.extents.x + Velocity.x) < horray.point.x)
- || (Velocity.x > 0f && (transform.position.x + boxcc.bounds.extents.x + Velocity.x) > horray.point.x))
- {
- SnapToWall(horray.point);
- Debug.Log ("Snapped");
- }
- }
- }
- //
- //Check collision above yourself
- //
- if (Velocity.y > 0f)
- {
- RaycastHit2D aboveray;
- Vector2 aboveboxbounds = boxcc.bounds.size;
- aboveboxbounds.y /= 1.1f;
- aboveboxbounds.x /= 1.1f;
- aboveray = Physics2D.BoxCast (transform.position, aboveboxbounds, 0f, Vector2.up, chkdistance);
- if (aboveray.collider != null)
- {
- if (aboveray.point.y < (transform.position.y + boxcc.bounds.extents.y + Velocity.y))
- {
- SnapToCeiling(aboveray.point);
- Debug.Log ("Ceiling snapped");
- }
- }
- }
- //Apply velocity
- transform.position += Velocity;
- lastpos = transform.position;
- //Debug
- }
- void SnapToCeiling(Vector3 r)
- {
- Vector3 ceiling = r;
- ceiling.y = ceiling.y - boxcc.bounds.extents.y;
- transform.Translate (0f, ceiling.y - transform.position.y, 0f);
- Velocity.y = 0f;
- }
- void SnapToWall(Vector3 r)
- {
- Vector3 wall = r;
- wall.x += wall.x < transform.position.x ? boxcc.bounds.extents.x : -boxcc.bounds.extents.x;
- transform.Translate (wall.x - transform.position.x, 0f, 0f);
- Velocity.x = 0f;
- }
- int[] RaydirX = { 1, 0, -1, 0 };
- int[] RaydirY = {0, 1, 0, -1 };
- int[] Raydir2X = { 1, 1, -1, -1 };
- int[] Raydir2Y = {-1, 1, 1, -1 };
- // Update is called once per frame
- void Update () {
- Time.timeScale = ts;
- float range = 3f;
- RaycastHit2D[] ray;
- for (int i = 0; i < 4; ++i)
- {
- Vector3 dir = new Vector2(RaydirX[i], RaydirY[i]).normalized;
- ray = Physics2D.RaycastAll(transform.position, dir, range);
- RaycastHit2D closest = ClosestRaycastAll(ray);
- Vector2 p = Vector2.zero;
- if (closest.collider != null)
- {
- p = closest.point;
- lr.SetPosition (i*2 + 1, p);
- }
- else
- {
- p = transform.position + (dir * range);
- lr.SetPosition (i*2 + 1, p);
- }
- debug[i].position = p;
- Vector3 dir2 = new Vector2(Raydir2X[i], Raydir2Y[i]).normalized;
- ray = Physics2D.RaycastAll(transform.position, dir2, range/2f);
- closest = ClosestRaycastAll(ray);
- if (closest.collider != null)
- {
- p = closest.point;
- lr.SetPosition (i*2, p);
- }
- else
- {
- p = transform.position + (dir2 * (range/2f));
- lr.SetPosition (i*2, p);
- }
- if (i == 0)
- lr.SetPosition(8, p);
- }
- if (Input.GetButtonDown("Fire1") && Grounded)
- {
- AddVelocity (Vector3.up * JumpHeight);
- Grounded = false;
- }
- haxis = Input.GetAxisRaw("Horizontal");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement