Advertisement
Guest User

PlayerScript

a guest
Dec 15th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(CharacterController))]
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     // Handling
  8.     public float rotationSpeed = 450;
  9.     public float walkSpeed = 5;
  10.     public float runSpeed = 8;
  11.  
  12.     // System
  13.     private Quaternion targetRotation;
  14.     public bool controllerInput = false;
  15.    
  16.  
  17.     // Components
  18.     public Gun gun;
  19.     private CharacterController controller;
  20.     private Camera cam;
  21.  
  22.     void Start() {
  23.         controller = GetComponent<CharacterController>();
  24.         cam = Camera.main;
  25.  
  26.  
  27.     }
  28.  
  29.     void Update() {
  30.  
  31.         if (controllerInput == true){
  32.  
  33.             ControlJoyPad();
  34.  
  35.         }else {
  36.  
  37.             ControlMouse();
  38.        
  39.         }
  40.            
  41.  
  42.  
  43.     }
  44.  
  45.     void ControlMouse() {
  46.  
  47.         Vector3 mousePos = Input.mousePosition;
  48.         mousePos = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.transform.position.y - transform.position.y));
  49.         targetRotation = Quaternion.LookRotation(mousePos - new Vector3(transform.position.x, 0, transform.position.z));
  50.         transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
  51.  
  52.         Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
  53.         Vector3 motion = input;
  54.         motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1) ? .7f : 1;
  55.         motion *= (Input.GetButton("Run")) ? runSpeed : walkSpeed;
  56.         motion += Vector3.up * -8;
  57.  
  58.         controller.Move(motion * Time.deltaTime);
  59.  
  60.         if (Input.GetButtonDown("Shoot"))
  61.         {
  62.             gun.Shoot();
  63.  
  64.         }
  65.  
  66.     }
  67.  
  68.     void ControlJoyPad(){
  69.  
  70.         Vector3 look = new Vector3(Input.GetAxisRaw("LHorizontal"), 0, Input.GetAxisRaw("LVertical"));
  71.  
  72.         if (look != Vector3.zero)
  73.         {
  74.             targetRotation = Quaternion.LookRotation(look);
  75.             transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
  76.         }
  77.  
  78.         Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
  79.         Vector3 motion = input;
  80.         motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1) ? .7f : 1;
  81.         motion *= (Input.GetButton("Run")) ? runSpeed : walkSpeed;
  82.         motion += Vector3.up * -8;
  83.  
  84.         controller.Move(motion * Time.deltaTime);
  85.  
  86.         if (Input.GetAxisRaw("Shoot")< 0)
  87.         {
  88.             gun.Shoot();
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement