SHARE
TWEET

Untitled

a guest Jan 25th, 2020 74 in 332 days
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 PlayerMovement : MonoBehaviour
  6. {
  7.     private CharacterController characterController;
  8.  
  9.     [SerializeField] float jumpSpeed = 20f;
  10.     [SerializeField] float gravity = 1f;
  11.     [SerializeField] float moveSpeed = 5f;
  12.  
  13.     float yVelocity = 0f;
  14.  
  15.     public float moveHori; // Tieto premenné sú použité na uloženie Inputu
  16.     public float moveVert;
  17.    
  18.     void Start() // Start is called before the first frame update
  19.     {
  20.         characterController = GetComponent<CharacterController>(); // Uložíme si CharacterController do premennej, aby sme ho neskôr mohli využiť
  21.     }
  22.  
  23.    
  24.     void Update() // Update is called once per frame
  25.     {
  26.         moveHori = Input.GetAxis("Horizontal"); // Získavanie Inputu
  27.         moveVert = Input.GetAxis("Vertical");
  28.  
  29.         Vector3 direction = new Vector3(moveHori, 0, moveVert); //Vector
  30.         Vector3 velocity = direction * moveSpeed;
  31.  
  32.         if (characterController.isGrounded)
  33.         {
  34.             if (Input.GetButtonDown("Jump"))
  35.             {
  36.                 yVelocity = jumpSpeed;
  37.             }
  38.         }
  39.         else
  40.         {
  41.             yVelocity -= gravity;
  42.         }
  43.         velocity.y = yVelocity;
  44.  
  45.         velocity = transform.TransformDirection(velocity);
  46.  
  47.         characterController.Move(velocity * Time.deltaTime);
  48.     }
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top