Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.62 KB | None | 0 0
  1. //bug that locks left strafe somehow
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7.  
  8. public class MoveAndMouselookLRScript : MonoBehaviour
  9. {
  10.     private Rigidbody rb;
  11.  
  12.     public float speed = 2000; //old remove?
  13.     public float jumpForce = 500;
  14.     bool jumping = false;
  15.     private float maxSpeedX = 10;
  16.     //private float maxSpeedY = 2000;
  17.     private float maxSpeedZ = 10;
  18.     private float runMultiplyer;
  19.     private float godModeSpeed;
  20.  
  21.     private float xForce;
  22.     private float zForce;
  23.     [NonSerialized] public Vector3 force;
  24.  
  25.     private float pitch = 0.0F;
  26.     private float yaw = 0.0F;
  27.  
  28.     [NonSerialized] public FacingDirection playerFacingDir;
  29.    
  30.     private float speedCap;
  31.  
  32.     private float mouseSensitivityX;
  33.  
  34.     [NonSerialized] public bool onGround;
  35.  
  36.     private int countMouse1 = 0;
  37.  
  38.     //private Vector3 platformForce;
  39.  
  40.     //Collider hanglingCol; not used rn
  41.  
  42.     [NonSerialized] public Vector3 cameraForward;
  43.     [NonSerialized] public Vector3 cameraRight;
  44.  
  45.     //[SerializeField] List<Mapping> mapping; use this to define manually
  46.     //FacingDirection FacingDirection; //predefined in enum
  47.  
  48.     void Start()
  49.     {
  50.         //hanglingCol = gameObject.transform.Find("ArmsPos").GetComponent<MeshCollider>(); not used rn
  51.  
  52.         rb = GetComponent<Rigidbody>();
  53.  
  54.         mouseSensitivityX = GameManager.gameManagerInstance.mouseSensitivityX;
  55.  
  56.         Cursor.visible = false;
  57.     }
  58.  
  59.     private void Update()
  60.     {
  61.         //print("click count" + clicked);
  62.  
  63.         //camera rotation
  64.         cameraForward = GetComponentInChildren<Camera>().transform.forward;
  65.         cameraRight = GetComponentInChildren<Camera>().transform.right;
  66.  
  67.         //y not needed and needs to be 0
  68.         cameraForward.y = 0;
  69.         cameraRight.y = 0;
  70.  
  71.         //dono need to reasearch
  72.         cameraForward = cameraForward.normalized;
  73.         cameraRight = cameraRight.normalized;
  74.  
  75.         //needs to be in update in order to function
  76.         onGround = GetComponentInChildren<GroundCheck>().onGround;
  77.  
  78.         //jump
  79.         if (Input.GetButtonDown("Jump") && onGround)
  80.         {
  81.             jumping = true;
  82.             Jump();
  83.         }
  84.  
  85.         //disable jumping status
  86.         if (onGround)
  87.         {
  88.             jumping = false;
  89.             //GameManager.gameManagerInstance.lockMousePlayerRotate = false;
  90.         }
  91.  
  92.         //enable god mode
  93.         if (Input.GetKeyUp(KeyCode.Alpha0))
  94.         {
  95.             print("0");
  96.             GameManager.gameManagerInstance.godModeEnabled = !GameManager.gameManagerInstance.godModeEnabled;
  97.         }
  98.         //camera oriantation + transomf orientation
  99.         //transform.position += (cameraForward * force.y + cameraRight * force.x) * Time.deltaTime;
  100.  
  101.         //godmode stuff
  102.         if (GameManager.gameManagerInstance.godModeEnabled)
  103.         {
  104.             if (Input.GetKey(KeyCode.E))
  105.             {
  106.                 force = new Vector3(0, 1, 0);
  107.             }
  108.             if (Input.GetKey(KeyCode.Q))
  109.             {
  110.                 force = new Vector3(0, -1, 0);
  111.             }
  112.  
  113.             // +speed
  114.             if (Input.GetKey("[+]"))
  115.             {
  116.                 print("+speed");
  117.                 godModeSpeed += 0.1f;
  118.             }
  119.             // -speed
  120.             if (Input.GetKey("[-]"))
  121.             {
  122.                 print("-speed");
  123.                 godModeSpeed -= 0.1f;
  124.  
  125.                 if (godModeSpeed < 1)
  126.                 {
  127.                     godModeSpeed = 1;
  128.                 }
  129.             }
  130.         }//god mode
  131.         else
  132.         {
  133.             godModeSpeed = 1;
  134.         }//end godmode
  135.  
  136.         // enable running
  137.         if (Input.GetKey(KeyCode.LeftShift) && onGround)
  138.         {
  139.             runMultiplyer = 1.1f;
  140.             speedCap = 25;
  141.         }
  142.         else if (onGround)
  143.         {
  144.             runMultiplyer = 1;
  145.             speedCap = 10;
  146.         }
  147.     }//end update
  148.  
  149.  
  150.     void FixedUpdate()
  151.     {
  152.         //lock/unlock mouse player rotation
  153.         if(GameManager.gameManagerInstance.lockMousePlayerRotate == false)
  154.         {
  155.             //reset to 0 if player turns 360 with mouse
  156.             if(yaw >= 70 || yaw <= -70){yaw = 0;}
  157.             else
  158.             {
  159.                 // mouse player rotation left/right
  160.                 yaw += Input.GetAxis("Mouse X");
  161.             }  
  162.         }
  163.         //left mousebutton counter
  164.         if (Input.GetMouseButtonDown(0))
  165.         {
  166.                 //Cursor.visible = false;
  167.                 //GameManager.gameManagerInstance.lockMousePlayerRotate = false;
  168.                 //Cursor.lockState = CursorLockMode.Locked;
  169.                 //countMouse1 = 0;
  170.         }
  171.         if (Input.GetMouseButtonDown(1))
  172.         {
  173.             countMouse1 ++;
  174.  
  175.             if(countMouse1 == 1)
  176.             {
  177.                 Cursor.visible = true;
  178.                 GameManager.gameManagerInstance.lockMousePlayerRotate = true;
  179.                 Cursor.lockState = CursorLockMode.None;
  180.             }
  181.             if(countMouse1 > 1)
  182.             {
  183.                 Cursor.visible = false;
  184.                 GameManager.gameManagerInstance.lockMousePlayerRotate = false;
  185.                 Cursor.lockState = CursorLockMode.Locked;
  186.                 countMouse1 = 0;
  187.             }
  188.         }
  189.         //left mousebutton counter end
  190.     }//end fixedUpdate
  191.  
  192.     void LateUpdate()
  193.     {
  194.         // rotate object to face mouse direction
  195.         rb.transform.localEulerAngles = new Vector3(0.0f, yaw * mouseSensitivityX, 0.0F);
  196.  
  197.         //print("yaw: " + yaw + "mouse axis: " + Input.GetAxis("Mouse X"));
  198.         if(yaw >= -5 && yaw <= 5) { playerFacingDir = FacingDirection.Forward; };
  199.         if(yaw >= -20 && yaw <= -5) { playerFacingDir = FacingDirection.Left; };
  200.         if(yaw >= -40 && yaw <= -20) { playerFacingDir = FacingDirection.Back; };
  201.         if(yaw >= -60 && yaw <= -40) { playerFacingDir = FacingDirection.Right; };
  202.         if(yaw >= -60 && yaw <= -60) { playerFacingDir = FacingDirection.Forward; };
  203.  
  204.         if(yaw >= -5 && yaw <= 5) { playerFacingDir = FacingDirection.Forward; };
  205.         if(yaw >= 5 && yaw <= 20) { playerFacingDir = FacingDirection.Right; };
  206.         if(yaw >= 20 && yaw <= 40) { playerFacingDir = FacingDirection.Back; };
  207.         if(yaw >= 40 && yaw <= 60) { playerFacingDir = FacingDirection.Left; };
  208.         if(yaw >= 60 && yaw <= 60) { playerFacingDir = FacingDirection.Forward; };
  209.  
  210.         //print("direction:" + playerFacingDir);
  211.     }//end late update
  212.  
  213.     private void Jump()
  214.     {
  215.         transform.Translate(Vector3.up * Time.deltaTime * jumpForce);
  216.     }//end jump
  217.  
  218. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement