using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public float groundCheckDistance = 0.1f; public float gravity; public float maxGravityVelocity= 0.01f; public Vector3 velocity = Vector3.zero; public Vector3 speed; public Vector3 maxVelocity; public float friction = 0.1f; void Start() { CameraFollower.Target=transform; } float input =0; void Update(){ input = Input.GetAxis("Horizontal"); } void FixedUpdate() { Gravity(); HandleInput(); transform.eulerAngles = Vector3.zero + new Vector3(0,0, (-velocity.x / maxVelocity.x) * 10); transform.position += velocity; } void HandleInput(){ if(input >0){ velocity = new Vector3(Mathf.Clamp(velocity.x + speed.x, -maxVelocity.x, maxVelocity.x), Mathf.Clamp(velocity.y + speed.y, -maxVelocity.y, maxVelocity.y)); }else if(input < 0){ velocity = new Vector3(Mathf.Clamp(velocity.x - speed.x, -maxVelocity.x, maxVelocity.x), Mathf.Clamp(velocity.y + speed.y, -maxVelocity.y, maxVelocity.y)); }else{ velocity = Vector3.Lerp(velocity, new Vector3(0, (velocity.y > 0) ? 0 : velocity.y),friction); } } void Gravity(){ RaycastHit2D groundHit = Physics2D.Linecast(transform.position, transform.position - new Vector3(0,groundCheckDistance,0)); if(groundHit.collider != null){ Debug.DrawLine(transform.position, groundHit.point, Color.green); float dist = transform.position.y - groundHit.point.y; velocity = new Vector3(velocity.x,groundCheckDistance - dist,velocity.z); }else{ velocity = new Vector3(velocity.x,Mathf.Lerp(velocity.y, -maxGravityVelocity,gravity),velocity.z); } } }