Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7. public float moveSpeed = 5f;
  8.  
  9. public Rigidbody2D rbGun;
  10. public Rigidbody2D rb;
  11. public Camera cam;
  12.  
  13. Vector2 movement;
  14. Vector2 mousePos;
  15.  
  16. void Update()
  17. {
  18. movement.x = Input.GetAxisRaw("Horizontal");
  19. movement.y = Input.GetAxisRaw("Vertical");
  20.  
  21. mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
  22.  
  23. Walk();
  24. }
  25.  
  26. private void Walk()
  27. {
  28. rb.MovePosition(rb.position + movement * moveSpeed * Time.deltaTime);
  29.  
  30. // Aim();
  31. }
  32.  
  33. public void Aim()
  34. {
  35. mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
  36.  
  37. Vector2 lookDir = mousePos - rbGun.position;
  38.  
  39. float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
  40. rbGun.rotation = angle;
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement