Advertisement
crystalguy123

Untitled

Jan 30th, 2020
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7. // Start is called before the first frame update
  8.  
  9. [SerializeField] private Vector3 speedXY;
  10. private Vector3 moveXY;
  11. private float timeFrame;
  12. private Rigidbody2D rigidbody2D;
  13. void Start()
  14. {
  15. transform.position = new Vector3 (0, 0, 0);
  16.  
  17.  
  18. }
  19.  
  20. private void Awake()
  21. {
  22. rigidbody2D = transform.GetComponent<rigidbody2D>();
  23. }
  24.  
  25.  
  26. // Update is called once per frame
  27. void Update()
  28. {
  29. Movement();
  30. if (Input.GetKeyDown(KeyCode.Space))
  31. {
  32. float jumpVelocity = 10f;
  33.  
  34. rigidbody2D.velocity = Vector2.up * jumpVelocity;
  35. }
  36.  
  37.  
  38.  
  39. }
  40.  
  41. private void Movement()
  42. {
  43. timeFrame = Time.deltaTime;
  44. moveXY.x = Input.GetAxis("Horizontal");
  45. speedXY.x = 5f;
  46. moveXY.y = Input.GetAxis("Vertical");
  47. speedXY.y = 5f;
  48. transform.Translate(moveXY.x * speedXY.x * timeFrame, moveXY.y * speedXY.y * timeFrame, 0);
  49. if(transform.position.x > 11.3f)
  50. {
  51. transform.position = new Vector3(-11.3f, transform.position.y, 0);
  52. }
  53. else if (transform.position.x < -11.3f)
  54. {
  55. transform.position = new Vector3(11.3f, transform.position.y, 0);
  56. }
  57.  
  58.  
  59.  
  60.  
  61. }
  62.  
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement