Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody2D))]
- [RequireComponent(typeof(BoxCollider2D))]
- public class SpielerController : MonoBehaviour {
- [Range( -50f, 50f)] public float gravity = 9.81f * 3f;
- [Range( 0f, 20f)] public float movementSpeed = 8.0f;
- [Range( 0f, 100f)] public float maxSpeed = 10.0f;
- [Range( 0f, 1000f)] public float fallSpeed = 50.0f;
- [Range( 0f, 30f)] public float jumpForceMultiplier = 2.5f;
- [Range( 0.01f, 2.00f)] public float jumpTimeout = 0.35f;
- private float lastJumpStart = -2.0f;
- [Range( 0f, 1f)] public float groundFriction = 0.7f;
- [Range( 0f, 1f)] public float groundCheckRadius = 0.15f;
- [Range( 2, 20)] public int groundRays = 5;
- public LayerMask groundCheckMask = new LayerMask();
- [Range( 0f, 1f)] public float airFriction = 0.1f;
- [Range( 0f, 1f)] public float wallCheckRadius = 0.15f;
- [Range( 2, 20)] public int wallRays = 5;
- public LayerMask wallCheckMask = new LayerMask();
- [Range( 0.1f, 9.0f)] public float forceRotationMultiplicator = 1.5f;
- [Range(0.001f,0.100f)] public float deathZone = 0.002f;
- [Range( 0f, 1f)] public float waterFriction = 0.5f;
- public SpielerStatus state = new SpielerStatus();
- public void Update() {
- Rigidbody2D rb = GetComponent<Rigidbody2D>();
- BoxCollider2D bc = GetComponent<BoxCollider2D>();
- startDetections(rb, bc);
- addMovingForce(rb);
- addRotationForce(rb);
- calculateFacingDirection(rb);
- }
- public void startDetections(Rigidbody2D rb, BoxCollider2D bc) {
- groundCheck(rb, bc);
- wallCheck(rb, bc);
- }
- public void wallCheck(Rigidbody2D rb, BoxCollider2D bc) {
- float h = (bc.size.y * bc.transform.localScale.y) * 0.955f;
- float checkStep = h / (float)(wallRays - 1);
- List<RaycastHit2D> raysLeft = new List<RaycastHit2D>();
- List<RaycastHit2D> raysRight = new List<RaycastHit2D>();
- for(int i = 0;i < wallRays;i++) {
- Vector2 startPointL = (transform.rotation * new Vector2(0f - (bc.size.x * bc.transform.localScale.x) / 2f, (checkStep * (float)i) - (h / 2f))) + new Vector3(bc.transform.position.x - 0.01f, bc.transform.position.y, 0f);
- Vector2 startPointR = (transform.rotation * new Vector2(0f + (bc.size.x * bc.transform.localScale.x) / 2f, (checkStep * (float)i) - (h / 2f))) + new Vector3(bc.transform.position.x + 0.01f, bc.transform.position.y, 0f);
- raysLeft.Add(Physics2D.Raycast(startPointL, transform.rotation * -Vector2.right, wallCheckRadius, wallCheckMask));
- raysRight.Add(Physics2D.Raycast(startPointR, transform.rotation * Vector2.right, wallCheckRadius, wallCheckMask));
- Debug.DrawRay(startPointL, (transform.rotation * -Vector2.right) * wallCheckRadius, Color.blue);
- Debug.DrawRay(startPointR, (transform.rotation * Vector2.right) * wallCheckRadius, Color.blue);
- }
- int hitCountL = 0;
- int hitCountR = 0;
- foreach(RaycastHit2D hit in raysLeft) {
- if(hit.collider != null) {
- hitCountL += 1;
- }
- }
- foreach(RaycastHit2D hit in raysRight) {
- if(hit.collider != null) {
- hitCountR += 1;
- }
- }
- state.wallLeft = (hitCountL > 0);
- state.wallRight = (hitCountR > 0);
- if(state.wallLeft) {
- if(!state.wasWallLeft) {
- // HitWallLeft
- // HitWall
- }
- } else {
- if(state.wasWallLeft) {
- // LeaveWallLeft
- // LeaveWall
- }
- }
- if(state.wallRight) {
- if(!state.wasWallRight) {
- // HitWallRight
- // HitWall
- }
- } else {
- if(state.wasWallRight) {
- // LeaveWallRight
- // LeaveWall
- }
- }
- state.wasWallLeft = state.wallLeft;
- state.wasWallRight = state.wallRight;
- }
- public void groundCheck(Rigidbody2D rb, BoxCollider2D bc) {
- float w = (bc.size.x * bc.transform.localScale.x) * 0.9985f;
- float checkStep = w / (float)(groundRays - 1);
- List<RaycastHit2D> rays = new List<RaycastHit2D>();
- for(int i = 0;i < groundRays;i++) {
- Vector2 startPoint = (transform.rotation * new Vector2((checkStep * (float)i) - (w / 2f), 0f - (bc.size.y * bc.transform.localScale.y) / 2f)) + new Vector3(bc.transform.position.x, bc.transform.position.y - 0.01f, 0f);
- rays.Add(Physics2D.Raycast(startPoint, transform.rotation * -Vector2.up, groundCheckRadius, groundCheckMask));
- Debug.DrawRay(startPoint, (transform.rotation * -Vector2.up) * groundCheckRadius, Color.red);
- }
- int hitCount = 0;
- foreach(RaycastHit2D hit in rays) {
- if(hit.collider != null) {
- hitCount += 1;
- }
- }
- state.grounded = (hitCount > 0);
- if(state.grounded) {
- if(!state.wasGrounded) {
- state.jumped = false;
- }
- } else {
- if(state.wasGrounded) {
- // LeaveGround
- }
- }
- state.wasGrounded = (hitCount > 0);
- }
- private Vector2 injectInputs(Rigidbody2D rb, Vector2 direction) {
- float vl = rb.velocity.x;
- float vlAbs = Mathf.Abs(vl);
- int hRaw = (int)Mathf.Round(Input.GetAxisRaw("Horizontal"));
- int vRaw = (int)Mathf.Round(Input.GetAxisRaw("Vertical"));
- direction.x += hRaw * movementSpeed;
- if(vRaw > 0 && state.grounded && !state.jumped && Time.time - lastJumpStart >= jumpTimeout) {
- lastJumpStart = Time.time;
- direction.y = (jumpForceMultiplier * 5f) * gravity;
- }
- return(direction);
- }
- private void clampVelocity(Rigidbody2D rb, Vector2 direction) {
- if(state.grounded) {
- if(Mathf.Abs(direction.x) == 0f) {
- rb.velocity = new Vector2(rb.velocity.x * (1f - groundFriction), rb.velocity.y);
- }
- }
- rb.velocity = new Vector2(Mathf.Clamp(rb.velocity.x, -movementSpeed, movementSpeed), Mathf.Min(fallSpeed, rb.velocity.y));
- }
- private void addMovingForce(Rigidbody2D rb) {
- Vector2 direction = new Vector2(0f, 0f);
- direction = injectInputs(rb, direction);
- rb.AddForce(direction);
- rb.AddForce(new Vector2(0f, -gravity));
- clampVelocity(rb, direction);
- }
- private void addRotationForce(Rigidbody2D rb) {
- Vector3 ea = rb.transform.rotation.eulerAngles;
- float posRot = Mathf.Round(ea.z * 100f) / 100f;
- float negRot = (0f - (180f + (360f - posRot))) + 180f;
- float rot = posRot;
- if(posRot > 180f) {
- rot = negRot;
- }
- float rotAbs = Mathf.Abs(rot) / 180f;
- float rotForce = -360f * (rot / 360f) * Time.deltaTime * (forceRotationMultiplicator * (0.2f + (rotAbs * 0.8f)));
- rb.AddTorque(rotForce, ForceMode2D.Force);
- }
- private void calculateFacingDirection(Rigidbody2D rb) {
- float vl = rb.velocity.x;
- int movingDirection = 0;
- int velocityDirection = 0;
- int hRaw = (int)Mathf.Round(Input.GetAxisRaw("Horizontal"));
- if(hRaw < 0) {
- movingDirection -= 1;
- }
- if(hRaw > 0) {
- movingDirection += 1;
- }
- if(Mathf.Abs(rb.velocity.x) > deathZone) {
- velocityDirection = 1;
- if(rb.velocity.x < 0) {
- velocityDirection = -1;
- }
- }
- state.facingLeft = movingDirection == -1;
- state.facingRight = movingDirection == 1;
- state.movingLeft = velocityDirection == -1;
- state.movingRight = velocityDirection == 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement