bono_u2

Untitled

Oct 20th, 2020
841
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class PlayerControls : MonoBehaviour
  2. {
  3.     [SerializeField] private Joystick joystick = null;
  4.     [SerializeField] private float speed = 0;
  5.     [SerializeField] private float rotZ = 0;
  6.     [SerializeField] private float offset = 0;
  7.     [SerializeField] private FieldOfView fieldOfView = null;
  8.  
  9.     private Vector2 moveInput = default;
  10.     private Vector2 moveVelocity = default;
  11.     private Rigidbody2D rb = null;
  12.     private PlayerControls Player = null;
  13.  
  14.     private void Start()
  15.     {
  16.         Player = GetComponent<PlayerControls>();  
  17.         rb = GetComponent<Rigidbody2D>();
  18.     }
  19.    
  20.     private void Update()
  21.     {
  22.        moveInput = new Vector2(joystick.Horizontal, joystick.Vertical);
  23.        moveVelocity = moveInput.normalized * speed;
  24.  
  25.         if (Mathf.Abs(joystick.Horizontal) > 0.3f || Mathf.Abs(joystick.Vertical) > 0.3f)
  26.         {    
  27.             rotZ = Mathf.Atan2(joystick.Vertical, joystick.Horizontal) * Mathf.Rad2Deg;
  28.         }
  29.         transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
  30.  
  31.         fieldOfView.SetAimDirection(new Vector3(moveInput.x, moveInput.y, 0f));
  32.         fieldOfView.SetOrigin(transform.position);
  33.     }
  34.    
  35.     private void FixedUpdate()
  36.     {
  37.         rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
  38.     }
  39. }
RAW Paste Data