Tarodev

Simple Player Movement - PlayerController.cs

Jun 14th, 2021
1,265
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     [SerializeField] private float _speed = 1;
  8.     [SerializeField] private Rigidbody _rb;
  9.     [SerializeField] private float _jumpForce = 300;
  10.    
  11.     void Update() {
  12.         var vel = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")) * _speed;
  13.         vel.y = _rb.velocity.y;
  14.         _rb.velocity = vel;
  15.  
  16.         if(Input.GetKeyDown(KeyCode.Space)) _rb.AddForce(Vector3.up * _jumpForce);
  17.     }
  18. }
RAW Paste Data