Advertisement
GauHelldragon

Untitled

Feb 28th, 2022
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class P1Script : MonoBehaviour
  6. {
  7.     public Rigidbody rb;
  8.     public float speed = 2.5f;
  9.     public float jumpPower = 5.0f;
  10.     private bool left = false;
  11.     private bool right = false;
  12.     private bool down = false;
  13.     private bool up = false;
  14.     private bool jumpKey = false;
  15.     private float velH = 0.0f;
  16.     private float velV = 0.0f;
  17.     public bool canJump = false;
  18.  
  19.  
  20.     // Start is called before the first frame update
  21.     void Start()
  22.     {
  23.         rb = GetComponent<Rigidbody>();
  24.  
  25.  
  26.     }
  27.  
  28.     // Update is called once per frame
  29.     void Update()
  30.     {
  31.         if (Input.GetKey(KeyCode.A))
  32.         {
  33.             left = true;            
  34.         }
  35.         if (Input.GetKeyUp(KeyCode.A))
  36.         {
  37.             left = false;            
  38.         }
  39.         if (Input.GetKey(KeyCode.D))
  40.         {
  41.             right = true;
  42.         }
  43.         if (Input.GetKeyUp(KeyCode.D))
  44.         {
  45.             right = false;
  46.         }
  47.  
  48.         velH = 0;
  49.         if ( left )
  50.         {
  51.             velH++;
  52.         }
  53.         if (right)
  54.         {
  55.             velH--;
  56.         }
  57.  
  58.         if (Input.GetKey(KeyCode.W))
  59.         {
  60.             up = true;
  61.         }
  62.         if (Input.GetKeyUp(KeyCode.W))
  63.         {
  64.             up = false;
  65.         }
  66.         if (Input.GetKey(KeyCode.S))
  67.         {
  68.             down = true;
  69.         }
  70.         if (Input.GetKeyUp(KeyCode.S))
  71.         {
  72.             down = false;
  73.         }
  74.  
  75.         velV = 0;
  76.         if (up)
  77.         {
  78.             velV++;
  79.         }
  80.         if (down)
  81.         {
  82.             velV--;
  83.         }
  84.  
  85.         rb.velocity = (new Vector3(velV, 0, velH) * speed);
  86.  
  87.         if (Input.GetKeyDown(KeyCode.Space))
  88.         {
  89.             jumpKey = true;
  90.         }
  91.         if (Input.GetKeyUp(KeyCode.Space))
  92.         {
  93.             jumpKey = false;
  94.         }
  95.  
  96.         if ( jumpKey && canJump)
  97.         {
  98.             //rb.velocity += (new Vector3(0, 1, 0) * jumpPower);
  99.             rb.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
  100.         }
  101.     }
  102.  
  103.  
  104.  
  105.     public void OnCollisionEnter(Collision collision)
  106.     {
  107.         if (collision.collider.tag == "Ground")
  108.         {
  109.             canJump = true;
  110.         }
  111.     }
  112.  
  113.  
  114.     public void OnCollisionExit(Collision collision)
  115.     {
  116.         if (collision.collider.tag == "Ground")
  117.         {
  118.             canJump = false;
  119.         }
  120.     }
  121.  
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement