Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.IO.Compression;
- using NUnit.Framework;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour
- {
- private CharacterController ctrlr;
- public float speed = 12f;
- public float gravity = -9.81f * 2;
- public float jumpHeight = 3f;
- public float airjumps = 0f; // don't worry about this, I'll come back and deal with it in the future.
- public Transform groundCheck;
- public float groundDistance = 0.4f;
- public LayerMask groundMask;
- Vector3 velocity;
- public bool isGrounded;
- bool isMoving;
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- void Start()
- {
- ctrlr = GetComponent<CharacterController>();
- }
- // Update is called once per frame
- void Update()
- {
- // check if we're touching the ground
- isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
- if (isGrounded && velocity.y < 0)
- {
- velocity.y = -2f;
- }
- // map our inputs
- float x = Input.GetAxis("Horizontal");
- float z = Input.GetAxis("Vertical");
- // vector stuff
- Vector3 movement = transform.right * x + transform.forward * z;
- // proper movement
- ctrlr.Move(movement * speed * Time.deltaTime);
- if (Input.GetButtonDown("Jump") && (isGrounded || airjumps > 0))
- {
- velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
- }
- velocity.y += gravity * Time.deltaTime;
- ctrlr.Move(velocity * Time.deltaTime);
- Vector3 lastPosition = gameObject.transform.position;
- if (lastPosition != gameObject.transform.position && isGrounded == true)
- {
- isMoving = true;
- }
- else
- {
- isMoving = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment