Advertisement
Dieton

PlayerSP

Aug 27th, 2023 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Cinemachine;
  5. using UnityEngine.InputSystem;
  6. using UnityEngine.UI;
  7. using TMPro;
  8. using System.Globalization;
  9.  
  10. //player - Singe Player (non multyplayer)
  11. public class PlayerSP : MonoBehaviour
  12. {
  13.     //---- player Input System ----//
  14.     private MyInputs
  15.         myInputActions;
  16.     private MyInputs
  17.         myInputActionsGet
  18.     {
  19.         get
  20.         {
  21.             if (myInputActions != null)
  22.             {
  23.                 return myInputActions;
  24.             }
  25.             return myInputActions = new MyInputs();
  26.         }
  27.     }
  28.  
  29.     //---- Player Movement ----//
  30.     private float
  31.         moveSpeed = 1.5f;
  32.  
  33.     //---- Player Chatercter Controller ----//
  34.     private CharacterController
  35.             myCharacterController;
  36.     private float
  37.         gravity = 8.5f;
  38.  
  39.     //---- Player Animations system ----//
  40.     private Animator
  41.         animator;
  42.  
  43.     void Awake()
  44.     {
  45.         animator = GetComponent<Animator>();
  46.         myCharacterController = GetComponent<CharacterController>();
  47.     }
  48.  
  49.     // Start is called before the first frame update
  50.     void Start()
  51.     {
  52.        
  53.     }
  54.  
  55.     #region Input System
  56.     //==== Input Sytem ====//
  57.     public void PlayerInputSetup()
  58.     {
  59.         myInputActions = new MyInputs();
  60.         myInputActions.Player.Enable();
  61.  
  62.         //myInputActionsGet.Player.Jump.performed += ctx => JumpButton();
  63.         //myInputActionsGet.Player.Interact.performed += ctx => PickUpInteraction();
  64.         //myInputActionsGet.Player.Fire.performed += ctx => SpawnCodePrefab(0);
  65.     }
  66.  
  67.     private void OnEnable() => myInputActionsGet.Enable();
  68.  
  69.     private void OnDisable() => myInputActionsGet.Disable();
  70.  
  71.     #endregion
  72.  
  73.     // Update is called once per frame
  74.     void Update()
  75.     {
  76.         PlayerMove();
  77.     }
  78.  
  79.     //==== Player Movement ====//
  80.     private void PlayerMove()
  81.     {
  82.         /*
  83.          //old movemnet
  84.         transform.Translate
  85.         (
  86.             0,
  87.             0,
  88.             myInputActions.Player.Move.ReadValue<Vector2>().y * Time.deltaTime * moveSpeed//_input.move.y
  89.         );
  90.         */
  91.  
  92.         //rotation
  93.         transform.Rotate
  94.         (
  95.     0,
  96.     myInputActions.Player.Move.ReadValue<Vector2>().x * Time.deltaTime * 110.0f,// _input.move.x
  97.     0
  98.         );
  99.  
  100.         //movement
  101.         Vector3 moveDirection = transform.TransformDirection
  102.         (
  103.         0,
  104.         -gravity,
  105.         myInputActions.Player.Move.ReadValue<Vector2>().y * moveSpeed
  106.         );
  107.         myCharacterController.Move(moveDirection * Time.deltaTime);
  108.  
  109.         //animation
  110.         animator.SetFloat("Speed", myInputActions.Player.Move.ReadValue<Vector2>().y);
  111.     }
  112.  
  113.     // See Order of Execution for Event Functions for information on FixedUpdate() and Update() related to physics queries
  114.     void FixedUpdate()
  115.     {
  116.         // Bit shift the index of the layer (8) to get a bit mask
  117.         int layerMask = 1 << 8;
  118.  
  119.         // This would cast rays only against colliders in layer 8.
  120.         // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
  121.         layerMask = ~layerMask;
  122.  
  123.         RaycastHit hit;
  124.  
  125.         Vector3 transformPosition = transform.position;
  126.         Vector3 transformOffset = new Vector3
  127.             (
  128.             transformPosition.x,
  129.             transformPosition.y + 1,
  130.             transformPosition.z
  131.             );
  132.  
  133.        
  134.         // Does the ray intersect any objects excluding the player layer
  135.         if (Physics.Raycast(transformPosition , transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
  136.         {
  137.             Debug.DrawRay(transformOffset , transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
  138.             Debug.Log("Did Hit");
  139.         }
  140.         else
  141.         {
  142.             Debug.DrawRay(transformOffset , transform.TransformDirection(Vector3.forward) * 1000, Color.white);
  143.             Debug.Log("Did not Hit");
  144.         }
  145.        
  146.     }
  147.  
  148.     //==== FootSteps ====//
  149.     public void OnFootstep()
  150.     {
  151.         //Do Nothing
  152.         //I put this here because there is a function trigger in some of the animation and i dont feel like removing it from the aanimation
  153.         //Debug.Log("Foot Step");
  154.     }
  155. }
  156.  
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement