Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace Scripts
  6. {
  7.  
  8.     [RequireComponent(typeof(CharacterController))]
  9.     public class PlayerController : MonoBehaviour
  10.     {
  11.  
  12.         [SerializeField]
  13.         private float movementSpeed = 8.0f;
  14.         [SerializeField]
  15.         private float jumpSpeed = 8.0f;
  16.         [SerializeField]
  17.         private float gravity = 20.0f;
  18.         public float maxMovement = 3f;
  19.         //public GameObject dollyCart;
  20.  
  21.         //public float rotationSpeed = 1.0F;
  22.  
  23.         private Vector3 moveDirection = Vector3.zero;
  24.         private CharacterController controller;
  25.  
  26.         void Start()
  27.         {
  28.             if (controller == null)
  29.                 controller = this.GetComponent<CharacterController>();
  30.         }
  31.  
  32.         void Update()
  33.         {
  34.             Move();
  35.         }
  36.  
  37.         void Move()
  38.         {
  39.             //transform.Rotate(new Vector3(0, Input.GetAxis("Horizontal"), 0) * rotationSpeed);
  40.  
  41.             //Debug.Log("Offset: " + transform.localPosition.x);
  42.             //Debug.Log("IsGorunded: " + controller.isGrounded);
  43.  
  44.             float inputH = Input.GetAxis("Horizontal");
  45.             float inputV = Input.GetAxis("Vertical");
  46.  
  47.             if (controller.isGrounded)
  48.             {
  49.                 if (transform.localPosition.x >= maxMovement)
  50.                 {
  51.                     if (inputH > 0)
  52.                     {
  53.                         inputH = 0;
  54.                     }
  55.                 }
  56.                 else if (transform.localPosition.x <= -maxMovement)
  57.                 {
  58.                     if (inputH < 0)
  59.                     {
  60.                         inputH = 0;
  61.                     }
  62.                 }
  63.  
  64.                 moveDirection = new Vector3(inputH, inputV, 0);
  65.                 moveDirection = transform.TransformDirection(moveDirection);
  66.                 moveDirection.x *= movementSpeed;
  67.  
  68.                 if (Input.GetButtonDown("Jump"))
  69.                 {
  70.                     moveDirection.y = jumpSpeed;
  71.                 }
  72.             }
  73.             else if (!controller.isGrounded)
  74.             {
  75.                 if (transform.localPosition.x >= maxMovement)
  76.                 {
  77.                     if (inputH > 0)
  78.                     {
  79.                         inputH = 0;
  80.                     }
  81.                 }
  82.                 else if (transform.localPosition.x <= -maxMovement)
  83.                 {
  84.                     if (inputH < 0)
  85.                     {
  86.                         inputH = 0;
  87.                     }
  88.                 }
  89.  
  90.                 moveDirection = new Vector3(inputH, moveDirection.y, 0);
  91.                 moveDirection = transform.TransformDirection(moveDirection);
  92.                 moveDirection.x *= movementSpeed;
  93.             }
  94.  
  95.             moveDirection.y -= gravity * Time.deltaTime;
  96.             controller.Move(moveDirection * Time.deltaTime);
  97.  
  98.         }
  99.  
  100.  
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement