Advertisement
Salasander

Velocity Player Movement

May 12th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerVelocityMovement : MonoBehaviour
  6. {
  7.     private Rigidbody rb2D;
  8.     public float movementSpeed;
  9.  
  10.     void Start()
  11.     {
  12.         rb2D = GetComponent<Rigidbody>();
  13.     }
  14.    
  15.     void Update()
  16.     {
  17.         //If "A" is being pressed...
  18.         if(Input.GetKey(KeyCode.A))
  19.         {
  20.             //Applies velocity to -1 on the x axis, 0 on the y axis, and 0 on the z axis
  21.             rb2D.velocity = new Vector3(-movementSpeed, 0, 0);
  22.         }
  23.  
  24.         //If "D" is being pressed...
  25.         if (Input.GetKey(KeyCode.D))
  26.         {
  27.             //Applies velocity to 1 on the x axis, 0 on the y axis, and 0 on the z axis
  28.             rb2D.velocity = new Vector3(movementSpeed, 0, 0);
  29.         }
  30.  
  31.         //If "S" is being pressed...
  32.         if (Input.GetKey(KeyCode.S))
  33.         {
  34.             //Applies velocity to 0 on the x axis, 0 on the y axis, and -1 on the z axis
  35.             rb2D.velocity = new Vector3(0, 0, -movementSpeed);
  36.         }
  37.  
  38.         //If "W" is being pressed...
  39.         if (Input.GetKey(KeyCode.W))
  40.         {
  41.             //Applies velocity to 0 on the x axis, 0 on the y axis, and 1 on the z axis
  42.             rb2D.velocity = new Vector3(0, 0, movementSpeed);
  43.         }
  44.  
  45.         //If "W" and "S" is not being pressed...
  46.         if(!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
  47.         {
  48.             //Get rid of any velocity on the z axis
  49.             rb2D.velocity = new Vector3(rb2D.velocity.x, rb2D.velocity.y, 0);
  50.         }
  51.  
  52.         //If "A" and "D" is not being pressed...
  53.         if (!Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
  54.         {
  55.             //Get rid of any movement on the x axis
  56.             rb2D.velocity = new Vector3(0, rb2D.velocity.y, rb2D.velocity.z);
  57.         }
  58.  
  59.         //If "W" and "A" and "S" and "D" is not being pressed...
  60.         if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D))
  61.         {
  62.             //Sets velocity to 0
  63.             rb2D.velocity = new Vector3(0, 0, 0);
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement