Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour
- {
- public CharacterController controller;
- public float speed;
- public float gravity = -19.81f; //default gravity
- public float jumpHeight = 3f;
- public Transform groundCheck; //invisible object on player's feet
- public float groundDistance = 0.2f; //radius of the invisible sphere
- public LayerMask groundMask; //controlling which objects the sphere should check for, so that it doesnt check isGrounded as true when standing on the player
- Vector3 velocity;
- bool isGrounded;
- // Update is called once per frame
- void Update()
- {
- isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
- //creates a tiny invisible sphere on player's foot with specified radius groundDistance, and if it collides with anything in groundMask, then isGrounded will set to true, if not, then false
- if (isGrounded && velocity.y < 0)
- {
- velocity.y = -2f; //not 0 but -2 is because if incase it sets true too soon, it makes the player force on ground properly
- }
- float x = Input.GetAxis("Horizontal"); //getaxis function gives 1 if right key is pressed
- float z = Input.GetAxis("Vertical"); //1 if forwards key is pressed
- /* first dumb idea - https://pastebin.com/CFXXtY8s*/
- Vector3 move = (transform.right * x + transform.forward * z) * speed; //transform right is red arrow vector, from -1 to 1, forward is blue axis
- //those arrows are multiplied by x or z, which are 0 if nothing is pressed or 1 if wasd is pressed
- //then its added together, forming a vector pointing to the direction where i wanna go
- //and multiplied by the speed constant
- controller.Move(move * Time.deltaTime);
- /*first dumb idea - gravity multiplier makes it grow more each time, but its would be better to square the velocity by gravity multiplier so it grows exponencialy
- velocity = velocity * gravityMultiplier;
- gravityMultiplier++;*/
- if (Input.GetButtonDown("Jump") && isGrounded) //jumping
- {
- velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); //why? physics https://imgur.com/a/BQMlYuj
- }
- velocity.y = velocity.y + gravity * Time.deltaTime;
- controller.Move(velocity * Time.deltaTime);
- //2x delta time - ^2, and why its like this - physics https://imgur.com/a/ulUTu7D
- //Debug.Log("transform.right " + transform.right);
- //Debug.Log("x " + x);
- //Debug.Log("transform.forward" + transform.forward);
- //Debug.Log("z " + z);
- //Debug.Log("move " + move);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement