Advertisement
lemansky

Untitled

Feb 26th, 2021
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMove : MonoBehaviour
  6. {
  7.     [SerializeField]
  8.     float _speed = 10.0f;
  9.     float _jumpForce = 20.0f;
  10.     Vector3 _movement;
  11.     Rigidbody _rb;
  12.     bool _canJump;
  13.     // Start is called before the first frame update
  14.  
  15.     void Start()
  16.     {
  17.         _rb = this.GetComponent<Rigidbody>();
  18.     }
  19.  
  20.     // Update is called once per frame
  21.     void Update()
  22.     {
  23.         if (Input.GetKeyDown(KeyCode.Space))
  24.         {
  25.             _canJump = true;
  26.         }
  27.     }
  28.  
  29.     private void FixedUpdate()
  30.     {
  31.         _movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  32.         if (_canJump)
  33.         {
  34.             _movement.y = _jumpForce;
  35.             _canJump = false;
  36.         }
  37.         _rb.AddForce(_movement * _speed);
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement