DevMerlin

Untitled

Jan 17th, 2021 (edited)
604
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. public class Player : MonoBehaviour
  4. {
  5.     public bool isReady = false;
  6.     public float m_moveSpeed = 0;
  7.     public float m_recordedSpeed = 0;
  8.     public Rigidbody2D pRB;
  9.  
  10.     public Transform m_Transform;
  11.  
  12.     public float overlapRadius = 3.5f;
  13.  
  14.     private Vector3 m_Velocity = Vector3.zero;
  15.  
  16.     [Range(0, .3f)]
  17.     [SerializeField]
  18.     private float m_Smoothing = .05f;
  19.  
  20.     public float objectsOnLeft = 0;
  21.     public float objectsOnRight = 0;
  22.  
  23.     public float rotationValue = 0;
  24.     public float rotationTarget = 5;
  25.  
  26.     // Start is called before the first frame update
  27.     void Start()
  28.     {
  29.         m_Transform = transform;
  30.         pRB = GetComponent<Rigidbody2D>();
  31.         m_recordedSpeed = m_moveSpeed;
  32.     }
  33.  
  34.     // Update is called once per frame
  35.     void FixedUpdate()
  36.     {
  37.         if (isReady) {
  38.             Vector2 targetVelocity = new Vector2(Input.GetAxis("Horizontal"), 0);
  39.             targetVelocity = m_Transform.TransformDirection(targetVelocity);
  40.             targetVelocity *= m_recordedSpeed * Time.deltaTime;
  41.  
  42.             Vector3 moveForce = Vector3.SmoothDamp(pRB.velocity, targetVelocity, ref m_Velocity, m_Smoothing);
  43.             pRB.velocity = moveForce;
  44.         }
  45.     }
  46.  
  47.     private void LateUpdate()
  48.     {
  49.         Vector3 pos = m_Transform.position;
  50.         pos.x = Mathf.Clamp(pos.x, -5f, 5f);
  51.         m_Transform.position = pos;
  52.     }
  53. }
RAW Paste Data