Created
August 29, 2024 12:36
-
-
Save Mike-Schvedov/d916f7ff3b6d8b5f082c15b85d14fe30 to your computer and use it in GitHub Desktop.
PlayerMovement - Survival Series Episode 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class PlayerMovement : MonoBehaviour | |
| { | |
| public CharacterController controller; | |
| public float speed = 12f; | |
| public float gravity = -9.81f * 2; | |
| public float jumpHeight = 3f; | |
| public Transform groundCheck; | |
| public float groundDistance = 0.4f; | |
| public LayerMask groundMask; | |
| Vector3 velocity; | |
| bool isGrounded; | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| //checking if we hit the ground to reset our falling velocity, otherwise we will fall faster the next time | |
| isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); | |
| if (isGrounded && velocity.y < 0) | |
| { | |
| velocity.y = -2f; | |
| } | |
| float x = Input.GetAxis("Horizontal"); | |
| float z = Input.GetAxis("Vertical"); | |
| //right is the red Axis, foward is the blue axis | |
| Vector3 move = transform.right * x + transform.forward * z; | |
| controller.Move(move * speed * Time.deltaTime); | |
| //check if the player is on the ground so he can jump | |
| if (Input.GetButtonDown("Jump") && isGrounded) | |
| { | |
| //the equation for jumping | |
| velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); | |
| } | |
| velocity.y += gravity * Time.deltaTime; | |
| controller.Move(velocity * Time.deltaTime); | |
| } | |
| } |
@AlexanderGorgonzola thanks man,i solve it coupls day ago.Problem was not in code how it strange.
@Yura2123e32e3 nice
thanks for your help in tutorials lol my prof sucks I didn't even learn any from his discussions
Your prof probably means well lmao
Hey @Yura2123e32e3 , how do you solved it?It also do not work by me
For those who come after. Unity changed something related to the input, so you have to revert it back to the old one.
To do that:
Edit--> Project settings--> Player--> Other Settings -->Active input Handling || and now choose either the "Old" or "Both"
Hey @Yura2123e32e3 , how do you solved it?It also do not work by me
Perhaps you forgot to put the GroundCheck at the bottom of the Cylinder. Try Debug.Log("Is Grounded: "+ isGrounded)?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Yura2123e32e3 , although I'm not too good at C#, I do know some stuff, so it would be nice if u could explain your situation, and maybe show the code.