Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- [RequireComponent(typeof(CharacterController))]
- public class PlayerMovement : MonoBehaviour {
- private CharacterController charCtrl;
- private Vector3 moveDirection = Vector3.zero;
- private bool hasJumped = false;
- public float MoveSpeed = 6f;
- public float JumpSpeed = 20f;
- public float Gravity = 2f;
- void Start() {
- charCtrl = GetComponent<CharacterController>();
- }
- void Update() {
- if (charCtrl.isGrounded) {
- moveDirection.x = Input.GetAxisRaw("Horizontal");
- moveDirection.y = 0f;
- moveDirection.z = Input.GetAxisRaw("Vertical");
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection.Normalize();
- moveDirection *= MoveSpeed;
- if (Input.GetButton("Jump")) {
- if (!hasJumped) {
- hasJumped = true;
- moveDirection.y = JumpSpeed;
- }
- } else if (hasJumped) {
- hasJumped = false;
- }
- }
- moveDirection.y += Gravity;
- charCtrl.Move(moveDirection * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement