Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour {
- private float xDir = 0;
- private float yDir = 0;
- private Rigidbody rigidbody;
- private bool controlsLocked = true;
- public float speed;
- [RangeAttribute (1, 10)]
- public float sensitivity;
- void Start() {
- rigidbody = GetComponent<Rigidbody> ();
- rigidbody.freezeRotation = true;
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- }
- void FixedUpdate () {
- if (!controlsLocked) {
- if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) {
- rigidbody.velocity = transform.right * speed;
- }
- if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)) {
- rigidbody.velocity = -transform.right * speed;
- }
- if (Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.W)) {
- rigidbody.velocity = transform.forward * speed;
- }
- if (Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.S)) {
- rigidbody.velocity = -transform.forward * speed;
- }
- if (Input.GetKey (KeyCode.Space)) {
- rigidbody.velocity = transform.up * speed;
- }
- if (Input.GetKey (KeyCode.LeftShift)) {
- rigidbody.velocity = -transform.up * speed;
- }
- xDir += Input.GetAxis ("Mouse X") * sensitivity;
- yDir -= Input.GetAxis ("Mouse Y") * sensitivity;
- rigidbody.rotation = Quaternion.Euler (yDir, xDir, 0.0f);
- }
- }
- void LockControls() {
- controlsLocked = true;
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- }
- void FreeControls() {
- controlsLocked = false;
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- void ResetPosition() {
- rigidbody.position = new Vector3 (-2, -2, -2);
- xDir = 0;
- yDir = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement