mirror of
				https://github.com/DerTyp7/defrain-shooter-unity.git
				synced 2025-10-29 20:52:10 +01:00 
			
		
		
		
	Added buggy PlayerController
This commit is contained in:
		
							
								
								
									
										15
									
								
								Assets/Scripts/DebugCanvas.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								Assets/Scripts/DebugCanvas.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using UnityEngine.UI; | ||||
| using TMPro; | ||||
| public class DebugCanvas : MonoBehaviour | ||||
| { | ||||
|     public TextMeshProUGUI DebugTextGrounded; | ||||
|     public GameObject Player; | ||||
|  | ||||
|     private void Update() | ||||
|     { | ||||
|         DebugTextGrounded.text = "isGrounded: " + Player.GetComponent<PlayerController>().isGrounded.ToString(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/DebugCanvas.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/DebugCanvas.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 836e95000cf92fa4b9f08812101723e7 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
| @@ -17,11 +17,14 @@ public class PlayerController : MonoBehaviour | ||||
|     [SerializeField] private float walkSpeed = 6.0f; | ||||
|     [SerializeField][Range(0.0f, 0.5f)] private float moveSmoothTime = 0.05f; | ||||
|     [SerializeField] float gravity = -13.0f; | ||||
|     [SerializeField] private float jumpForce; | ||||
|     [SerializeField] private float jumpMultiplier; | ||||
|     [SerializeField] private float jumpHeight; | ||||
|  | ||||
|     [Header("Ground Check")] | ||||
|     [SerializeField] private Transform groundCheck; | ||||
|     [SerializeField] private float groundDistance = 0.4f; | ||||
|     [SerializeField] private LayerMask groundMask; | ||||
|  | ||||
|     private bool isJumping; | ||||
|     public bool isGrounded; | ||||
|  | ||||
|     private float cameraPitch = 0f; | ||||
|     private float velocityY = 0.0f; | ||||
| @@ -46,8 +49,13 @@ public class PlayerController : MonoBehaviour | ||||
|     private void Update() | ||||
|     { | ||||
|         UpdateMouseLook(); | ||||
|         Grounded(); | ||||
|         UpdateMovement(); | ||||
|         JumpInput(); | ||||
|     } | ||||
|     private void Grounded() | ||||
|     { | ||||
|         //Check every frame if the player stands on the ground | ||||
|         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); | ||||
|     } | ||||
|     private void UpdateMouseLook() | ||||
|     { | ||||
| @@ -64,30 +72,32 @@ public class PlayerController : MonoBehaviour | ||||
|     } | ||||
|     private void UpdateMovement() | ||||
|     { | ||||
|  | ||||
|         //Jump | ||||
|         if (Input.GetButtonDown("Jump") && isGrounded) | ||||
|         { | ||||
|             Debug.Log("Jump"); | ||||
|             velocityY += Mathf.Sqrt(jumpHeight * -2f * gravity); | ||||
|         } | ||||
|  | ||||
|         //Grounded | ||||
|          | ||||
|         if (isGrounded) | ||||
|             velocityY = 0.0f; | ||||
|          | ||||
|          velocityY += gravity * Time.deltaTime; | ||||
|  | ||||
|  | ||||
|         Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); //Get Inputs | ||||
|         targetDir.Normalize(); //Damit schr<68>g laufen nicht schneller ist | ||||
|  | ||||
|         currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime); //Smooth movement change | ||||
|  | ||||
|         if (controller.isGrounded) | ||||
|             velocityY = 0.0f; | ||||
|  | ||||
|         velocityY += gravity * Time.deltaTime; | ||||
|  | ||||
|         Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY; | ||||
|         Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + (Vector3.up * velocityY); | ||||
|  | ||||
|         controller.Move(velocity * Time.deltaTime); | ||||
|  | ||||
|     } | ||||
|     private void JumpInput() | ||||
|     { | ||||
|         Debug.Log(controller.isGrounded); | ||||
|         if (Input.GetButtonDown("Jump") && controller.isGrounded) | ||||
|         { | ||||
|             isJumping = true; | ||||
|             controller.Move(Vector3.up * jumpForce * jumpMultiplier * Time.deltaTime); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|    | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 DerTyp187
					DerTyp187