Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class player_mvt : MonoBehaviour
- {
- Rigidbody2D rb;
- [Header("Mouvement")]
- public float maxSpeed;
- public float slipperiness;
- [Range(0,1)]
- public float mvtDamp, turnDamp, stopDamp;
- [Space(15)]
- public float jumpForce;
- public float jumpTime;
- float _jumpTime;
- [Range(0,1)]
- public float jumpCutMultiplier;
- [Range(1,2)]
- public float fallMultiplier;
- public float maxGravity, baseGravity;
- [Range(0,1)]
- public float airMvtDamp, airTurnDamp, airStopDamp;
- public bool noFlip;
- [Header("Collisions")]
- public Bounds groundCheck;
- public LayerMask Ground;
- public float mx, my;
- public bool isJump;
- float rota = 0;
- public Bounds platCheck;
- public Bounds platEnd;
- Coroutine dropCor;
- [Space(15)]
- public bool can_move = true;
- int accel = 0;
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- _jumpTime = jumpTime;
- }
- void FixedUpdate()
- {
- Xmove();
- Flip();
- Fall();
- }
- void Flip()
- {
- if(!can_move) return;
- if(noFlip) return;
- if(mx > 0.1f) rota = 0;
- if(mx < -0.1f) rota = 180;
- transform.rotation = Quaternion.Euler(0,rota,0);
- }
- void Fall()
- {
- if(rb.velocity.y <= 0 && !Grounded())
- {
- rb.gravityScale = Gravity(rb.gravityScale);
- }
- else
- {
- rb.gravityScale = baseGravity;
- }
- }
- float Gravity(float gravity)
- {
- if(gravity < baseGravity) return baseGravity;
- if(gravity > maxGravity) return maxGravity;
- return gravity * fallMultiplier;
- }
- void Update()
- {
- print(rb.velocity.x);
- }
- void Xmove()
- {
- if(!can_move) return;
- float veloc = rb.velocity.x;
- veloc += (float)mx;
- rb.velocity = new Vector2(veloc, rb.velocity.y);
- }
- float MoveDamp(float stop, float turn, float damp)
- {
- float hVeloc = rb.velocity.x;
- hVeloc += mx;
- // if (Mathf.Abs(hVeloc) > timst.mvtCalc(maxSpeed))
- // {
- // hVeloc = timst.mvtCalc(maxSpeed) * mx;
- // }
- // if (Mathf.Abs(mx) < 0.01f)
- // {
- // hVeloc *= Mathf.Pow(1f - stop, Time.fixedDeltaTime * slipperiness);
- // }
- // else if (Mathf.Sign(mx) != Mathf.Sign(hVeloc))
- // {
- // hVeloc *= Mathf.Pow(1f - turn, Time.fixedDeltaTime * slipperiness);
- // }
- // else
- // {
- // hVeloc *= Mathf.Pow(1f - damp, Time.fixedDeltaTime * slipperiness);
- // }
- return hVeloc;
- }
- public void JumpStart()
- {
- isJump = true;
- _jumpTime = jumpTime;
- rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
- }
- public void Jump()
- {
- if(_jumpTime <= 0) isJump = false;
- if(!isJump) return;
- if(!can_move) return;
- rb.velocity = new Vector2(rb.velocity.x, jumpForce);
- _jumpTime -= Time.deltaTime;
- }
- public void StopJump()
- {
- if(!can_move) return;
- isJump = false;
- if (rb.velocity.y > 0 && !Grounded())
- {
- rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * jumpCutMultiplier);
- }
- }
- public void Drop()
- {
- if(!can_move) return;
- LayerMask platmask = LayerMask.GetMask("Platform");
- Collider2D ray = Physics2D.OverlapBox(transform.position + platCheck.center, platCheck.extents,0, platmask);
- if(ray != null)
- {
- Physics2D.IgnoreLayerCollision(3, 11, true);
- if(dropCor != null) StopCoroutine(dropCor);
- dropCor = StartCoroutine(DropCor());
- }
- }
- IEnumerator DropCor()
- {
- yield return new WaitUntil(() => !PlatEnd());
- Physics2D.IgnoreLayerCollision(3, 11, false);
- dropCor = null;
- }
- public bool PlatEnd()
- {
- LayerMask platmask = LayerMask.GetMask("Platform");
- Collider2D ray = Physics2D.OverlapBox(transform.position + platEnd.center, platEnd.extents,0, platmask);
- if(ray != null)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public bool Grounded()
- {
- Collider2D ray = Physics2D.OverlapBox(transform.position + groundCheck.center, groundCheck.extents,0, Ground);
- if(ray != null)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void OnDrawGizmos()
- {
- Gizmos.color = Color.blue;
- Gizmos.DrawWireCube(transform.position + groundCheck.center, groundCheck.extents);
- Gizmos.color = Color.magenta;
- Gizmos.DrawWireCube(transform.position + platCheck.center, platCheck.extents);
- Gizmos.color = Color.yellow;
- Gizmos.DrawWireCube(transform.position + platEnd.center, platEnd.extents);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement