Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour
- {
- // Start is called before the first frame update
- [SerializeField] private Vector3 speedXY;
- private Vector3 moveXY;
- private float timeFrame;
- private Rigidbody2D rigidbody2D;
- void Start()
- {
- transform.position = new Vector3 (0, 0, 0);
- }
- private void Awake()
- {
- rigidbody2D = transform.GetComponent<rigidbody2D>();
- }
- // Update is called once per frame
- void Update()
- {
- Movement();
- if (Input.GetKeyDown(KeyCode.Space))
- {
- float jumpVelocity = 10f;
- rigidbody2D.velocity = Vector2.up * jumpVelocity;
- }
- }
- private void Movement()
- {
- timeFrame = Time.deltaTime;
- moveXY.x = Input.GetAxis("Horizontal");
- speedXY.x = 5f;
- moveXY.y = Input.GetAxis("Vertical");
- speedXY.y = 5f;
- transform.Translate(moveXY.x * speedXY.x * timeFrame, moveXY.y * speedXY.y * timeFrame, 0);
- if(transform.position.x > 11.3f)
- {
- transform.position = new Vector3(-11.3f, transform.position.y, 0);
- }
- else if (transform.position.x < -11.3f)
- {
- transform.position = new Vector3(11.3f, transform.position.y, 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement