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;
- 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 = GetComponent<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;
- lr.SetPosition (1, r);
- }
- 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;
- //
- //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));
- foreach (RaycastHit2D r in ray)
- {
- if (r.collider != boxcc)
- {
- Land (r.point);
- Debug.Log ("Landed ray");
- }
- }
- }
- //
- //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[0].position = groundcheckray.point;
- Debug.Log ("Landed box");
- }
- lr.SetPosition (1, groundcheckray.point);
- debug[1].position = groundcheckray.point;
- }
- }
- //Gravity
- if (!Grounded)
- AddVelocity (Physics2D.gravity * Time.deltaTime);
- //Horizontal movement
- if (GetVelocity ().x < (maxSpeed * haxis) && haxis > 0f)
- AddVelocity ( Vector3.right * Mathf.Min ( haxis * moveSpeed * Time.deltaTime, (maxSpeed - GetVelocity ().x) * haxis));
- if (GetVelocity ().x > (maxSpeed * haxis) && haxis < 0f)
- AddVelocity ( Vector3.right * Mathf.Max ( haxis * moveSpeed * Time.deltaTime, (-maxSpeed - GetVelocity ().x) * -haxis));
- //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;
- 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 - sideboxcheckbounds.x + Velocity.x) < horray.point.x)
- || (Velocity.x > 0f && (transform.position.x + sideboxcheckbounds.x + Velocity.x) > horray.point.x))
- {
- SnapToWall(horray.point);
- Debug.Log ("Snapped");
- }
- debug[2].position = horray.point;
- }
- }
- //
- //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))
- {
- Debug.Log (aboveray.point.y + " : " + (transform.position.y + boxcc.bounds.extents.y + Velocity.y));
- SnapToCeiling(aboveray.point);
- Debug.Log ("Ceiling snapped");
- }
- debug[3].position = aboveray.point;
- }
- }
- //Apply velocity
- transform.position += Velocity;
- lastpos = transform.position;
- //Debug
- lr.SetPosition(0, transform.position);
- }
- 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;
- }
- // Update is called once per frame
- void Update () {
- Time.timeScale = ts;
- if (Input.GetButtonDown("Fire1") && Grounded)
- {
- AddVelocity (Vector3.up * 10f);
- Grounded = false;
- }
- haxis = Input.GetAxisRaw("Horizontal");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement