Advertisement
johnnygoodguy2000

playerMovement.cs

Jun 15th, 2024
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Numerics; // Only if necessary for other parts of the script
  4. using UnityEngine;
  5. using UnityEngine.InputSystem;
  6.  
  7.  
  8. public class PlayerMovement : MonoBehaviour
  9. {
  10.     [SerializeField] private float speed;
  11.  
  12.     [SerializeField] private float rotationSpeed;
  13.  
  14.     [SerializeField] private float screenBorder;
  15.  
  16.     private Rigidbody2D rigidbody;
  17.     private UnityEngine.Vector2 movementInput;
  18.     private UnityEngine.Vector2 smoothedMovementInput;
  19.     private UnityEngine.Vector2 movementInputSmoothVelocity;
  20.     private Camera camera;
  21.  
  22.     private Animator animator;
  23.  
  24.     private void Awake()
  25.     {
  26.         rigidbody = GetComponent<Rigidbody2D>();
  27.         camera = Camera.main;
  28.  
  29.         animator = GetComponent<Animator>();
  30.     }
  31.  
  32.     private void FixedUpdate()
  33.     {
  34.         SetPlayerVelocity();
  35.         RotateInDirectionOfInput();
  36.         SetAnimation();
  37.     }
  38.  
  39.     private void SetAnimation()
  40.     {
  41.         bool isMoving = movementInput != UnityEngine.Vector2.zero;
  42.         animator.SetBool("isMoving", isMoving);
  43.     }
  44.  
  45.     private void SetPlayerVelocity()
  46.     {
  47.         smoothedMovementInput = UnityEngine.Vector2.SmoothDamp(smoothedMovementInput, movementInput, ref movementInputSmoothVelocity, 0.1f);
  48.         rigidbody.velocity = smoothedMovementInput * speed;
  49.         PreventPlayerGoingOffScreen();
  50.     }
  51.  
  52.     private void PreventPlayerGoingOffScreen()
  53.     {
  54.         UnityEngine.Vector2 screenPosition = camera.WorldToScreenPoint(transform.position);
  55.  
  56.         if ((screenPosition.x < screenBorder && rigidbody.velocity.x < 0) || (screenPosition.x > camera.pixelWidth - screenBorder && rigidbody.velocity.x > 0))
  57.         {
  58.             rigidbody.velocity = new UnityEngine.Vector2(0, rigidbody.velocity.y);
  59.  
  60.         }
  61.          if ((screenPosition.y < screenBorder && rigidbody.velocity.y < 0) || (screenPosition.y > camera.pixelHeight - screenBorder && rigidbody.velocity.y > 0))
  62.         {
  63.             rigidbody.velocity = new UnityEngine.Vector2(rigidbody.velocity.x, 0);
  64.  
  65.         }
  66.  
  67.  
  68.  
  69.     }
  70.  
  71.     private void RotateInDirectionOfInput()
  72.     {
  73.         if (movementInput != UnityEngine.Vector2.zero)
  74.         {
  75.             UnityEngine.Quaternion targetRotation = UnityEngine.Quaternion.LookRotation(transform.forward, smoothedMovementInput);
  76.             UnityEngine.Quaternion rotation = UnityEngine.Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
  77.  
  78.             rigidbody.MoveRotation(rotation);
  79.         }
  80.     }
  81.  
  82.     private void OnMove(InputValue inputValue)
  83.     {
  84.         movementInput = inputValue.Get<UnityEngine.Vector2>();
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement