Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerVelocityMovement : MonoBehaviour
- {
- private Rigidbody rb2D;
- public float movementSpeed;
- void Start()
- {
- rb2D = GetComponent<Rigidbody>();
- }
- void Update()
- {
- //If "A" is being pressed...
- if(Input.GetKey(KeyCode.A))
- {
- //Applies velocity to -1 on the x axis, 0 on the y axis, and 0 on the z axis
- rb2D.velocity = new Vector3(-movementSpeed, 0, 0);
- }
- //If "D" is being pressed...
- if (Input.GetKey(KeyCode.D))
- {
- //Applies velocity to 1 on the x axis, 0 on the y axis, and 0 on the z axis
- rb2D.velocity = new Vector3(movementSpeed, 0, 0);
- }
- //If "S" is being pressed...
- if (Input.GetKey(KeyCode.S))
- {
- //Applies velocity to 0 on the x axis, 0 on the y axis, and -1 on the z axis
- rb2D.velocity = new Vector3(0, 0, -movementSpeed);
- }
- //If "W" is being pressed...
- if (Input.GetKey(KeyCode.W))
- {
- //Applies velocity to 0 on the x axis, 0 on the y axis, and 1 on the z axis
- rb2D.velocity = new Vector3(0, 0, movementSpeed);
- }
- //If "W" and "S" is not being pressed...
- if(!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
- {
- //Get rid of any velocity on the z axis
- rb2D.velocity = new Vector3(rb2D.velocity.x, rb2D.velocity.y, 0);
- }
- //If "A" and "D" is not being pressed...
- if (!Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
- {
- //Get rid of any movement on the x axis
- rb2D.velocity = new Vector3(0, rb2D.velocity.y, rb2D.velocity.z);
- }
- //If "W" and "A" and "S" and "D" is not being pressed...
- if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D))
- {
- //Sets velocity to 0
- rb2D.velocity = new Vector3(0, 0, 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement