Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Scripts
- {
- [RequireComponent(typeof(CharacterController))]
- public class PlayerController : MonoBehaviour
- {
- [SerializeField]
- private float movementSpeed = 8.0f;
- [SerializeField]
- private float jumpSpeed = 8.0f;
- [SerializeField]
- private float gravity = 20.0f;
- public float maxMovement = 3f;
- //public GameObject dollyCart;
- //public float rotationSpeed = 1.0F;
- private Vector3 moveDirection = Vector3.zero;
- private CharacterController controller;
- void Start()
- {
- if (controller == null)
- controller = this.GetComponent<CharacterController>();
- }
- void Update()
- {
- Move();
- }
- void Move()
- {
- //transform.Rotate(new Vector3(0, Input.GetAxis("Horizontal"), 0) * rotationSpeed);
- //Debug.Log("Offset: " + transform.localPosition.x);
- //Debug.Log("IsGorunded: " + controller.isGrounded);
- float inputH = Input.GetAxis("Horizontal");
- float inputV = Input.GetAxis("Vertical");
- if (controller.isGrounded)
- {
- if (transform.localPosition.x >= maxMovement)
- {
- if (inputH > 0)
- {
- inputH = 0;
- }
- }
- else if (transform.localPosition.x <= -maxMovement)
- {
- if (inputH < 0)
- {
- inputH = 0;
- }
- }
- moveDirection = new Vector3(inputH, inputV, 0);
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection.x *= movementSpeed;
- if (Input.GetButtonDown("Jump"))
- {
- moveDirection.y = jumpSpeed;
- }
- }
- else if (!controller.isGrounded)
- {
- if (transform.localPosition.x >= maxMovement)
- {
- if (inputH > 0)
- {
- inputH = 0;
- }
- }
- else if (transform.localPosition.x <= -maxMovement)
- {
- if (inputH < 0)
- {
- inputH = 0;
- }
- }
- moveDirection = new Vector3(inputH, moveDirection.y, 0);
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection.x *= movementSpeed;
- }
- moveDirection.y -= gravity * Time.deltaTime;
- controller.Move(moveDirection * Time.deltaTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement