- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Animations;
- public class CharacterMovement : MonoBehaviour
- {
- private Rigidbody rb;
- [SerializeField]
- private Transform cameraTransform;
- [SerializeField]
- private int maxNumberOfJumps = 2;
- [HideInInspector]
- public int numberOfJumps { get; private set; } = 0;
- [SerializeField]
- private float GroundDis = 2f;
- [SerializeField]
- private float playerSpeed = 10f;
- private float noClipSpeed = 5f;
- [SerializeField]
- private float jumpForce = 1.0f;
- [SerializeField]
- private float gravityScale = 5f;
- [HideInInspector]
- public bool jump {get; private set;}
- private bool noClip;
- private ParticleSystem ps;
- private bool emitingParticles = false;
-
-
- [HideInInspector]
- public bool canMove = true;
-
- void Start()
- {
- jump = false;
- noClip = false;
- rb = gameObject.GetComponent<Rigidbody>();
- ps = GetComponentInChildren<ParticleSystem>();
- }
- private void Update()
- {
- if ((Input.GetButtonDown("Jump") && OnGround() == true) || (Input.GetButtonDown("Jump") && numberOfJumps < maxNumberOfJumps))
- {
- jump = true;
- }
- if (OnGround()) numberOfJumps = 0;
- }
-
- void FixedUpdate()
- {
- if (!PauseMenuBehaviour.isPaused)
- {
- float horizontal = (Input.GetAxis("Horizontal"));
- float vertical = (Input.GetAxis("Vertical"));
-
-
-
-
- if (!noClip)
- {
- rb.velocity = (new Vector3(horizontal * playerSpeed, 0, vertical * playerSpeed) * Time.fixedDeltaTime) + new Vector3(0, rb.velocity.y, 0);
- rb.velocity = Quaternion.AngleAxis(cameraTransform.rotation.eulerAngles.y, Vector3.up) * rb.velocity;
- rb.velocity.Normalize();
- if (jump == true && numberOfJumps < maxNumberOfJumps)
- {
-
- numberOfJumps++;
- jump = false;
- rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
- rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
- if (ps != null && emitingParticles)
- {
- ps.Play();
- Debug.Log("Particle");
- }
- }
- if (rb.useGravity)
- {
- rb.AddForce(gravityScale * Physics.gravity);
- }
- Vector3 moveVector = new Vector3(rb.velocity.normalized.x, 0, rb.velocity.normalized.z);
- transform.LookAt(transform.position + moveVector * 2f, Vector3.up);
- } else
- {
- if (horizontal != 0 || vertical != 0 || Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.LeftControl))
- {
- float jumpHeight = 0;
- if (Input.GetKey(KeyCode.Space))
- {
- jumpHeight = jumpForce;
- }
- if (Input.GetKey(KeyCode.LeftControl))
- {
- jumpHeight = -jumpForce;
- }
- Vector3 moveDir = Vector3.Normalize((transform.right * horizontal) + (transform.forward * vertical));
-
-
- transform.position += new Vector3(moveDir.x * (noClipSpeed * Time.fixedDeltaTime),jumpHeight * Time.fixedDeltaTime ,moveDir.z * (noClipSpeed * Time.fixedDeltaTime));
- }
- }
- }
- }
- private bool OnGround()
- {
- RaycastHit hit;
- bool status = Physics.Raycast(transform.position, Vector3.down,out hit, GroundDis);
- if (hit.collider != null &&hit.collider.gameObject.CompareTag("Bridge"))
- {
- return false;
- }
- emitingParticles = status;
- return status;
- }
- public void SetNoClip(bool state)
- {
- rb.useGravity = !state;
- rb.isKinematic = state;
- noClip = state;
- }
-
- }