Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Header("Player Movement")]
- public CharacterController controller;
- public float speed = 12f;
- public NavMeshAgent agent;
- [Header("Jumping")]
- public float gravity = -9.81f;
- public float jumpHeight = 3f;
- public Transform groundCheck;
- public float groundDistance = 0.4f;
- public LayerMask groundMask;
- Vector3 velocity;
- public bool isGrounded;
- [Header("Wall Running")]
- public bool isWallRunning;
- public Transform wallCheck;
- public float wallDistance = 0.02f;
- public float runningSpeed = 1f;
- public LayerMask runnableWall;
- Vector3 runningDirection;
- float currentRunTIme = maxRunTime;
- public const float maxRunTime = 1f;
- float runDistance = 7f;
- float runStoppingSpeed = 0.1f;
- private void Update()
- {
- 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");
- Vector3 move = transform.right * x + transform.forward * z;
- controller.Move(move * speed * Time.deltaTime);
- if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
- {
- velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
- FindObjectOfType<SoundController>().jumpingSound.Play();
- }
- velocity.y += gravity * Time.deltaTime;
- controller.Move(velocity * Time.deltaTime);
- if (isWallRunning == false)
- {
- gravity = -9.81f;
- }
- else if (isWallRunning == true)
- {
- gravity = 0f;
- }
- if (controller.isGrounded)
- {
- isWallRunning = false;
- }
- if (Physics.CheckSphere(wallCheck.position, wallDistance, runnableWall) == true && controller.isGrounded == false)
- {
- currentRunTIme = 0;
- StartCoroutine(StopGravity());
- FindObjectOfType<SoundController>().wallRunSound.Play();
- }
- if (currentRunTIme < maxRunTime)
- {
- runningDirection = transform.forward * runDistance;
- currentRunTIme += runStoppingSpeed;
- }
- else
- {
- runningDirection = Vector3.zero;
- }
- controller.Move(runningDirection * Time.deltaTime * runningSpeed);
- }
- IEnumerator StopGravity()
- {
- isWallRunning = true;
- yield return new WaitForSeconds(1);
- isWallRunning = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement