PlayerMovement

mail@pastecode.io avatar
unknown
csharp
2 years ago
4.1 kB
1
Indexable
Never
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    CharacterController controller;
    public Interactable focus;

    public float walkSpeed = 10f;
    public float runSpeed = 20f;
    public float currentSpeed;
    public Vector3 lastPosition;
    public float distance;

    private Vector3 velocity = Vector3.zero;
    public float jump = 10f;
    public float gravity = -9.8f;


    public Animator animator;
    public Camera cam;
    public Transform mainCamera;
    public Weapon weapon;


    // Start is called before the first frame update
    void Start()
    {

        controller = GetComponent<CharacterController>();

    }

    // Update is called once per frame
    void Update()
    {
        //Rolle machen
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            StartCoroutine(DoRoll());
        }


        // Schlagen
        if (Input.GetMouseButton(0))
        {
            StartCoroutine(weapon.Swing());
        }


        //Springen
        if (controller.isGrounded && Input.GetButtonDown("Jump"))
        {
            velocity.y = jump;

        }
        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);


        // Interact with Raycast RightClick
        if (Input.GetMouseButton(1))
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 100))
            {
                Interactable interactable = hit.collider.GetComponent<Interactable>();

                if (interactable != null)
                {
                    SetFocus(interactable);
                }
            }



            if (focus != null)
            {
                distance = Vector3.Distance(controller.GetComponent<Transform>().position, focus.GetComponent<Transform>().position);
                if (distance >= 30)
                {
                    RemoveFocus();
                }
            }



            float Horizontal = Input.GetAxis("Horizontal") * walkSpeed * Time.deltaTime;
            float Vertical = Input.GetAxis("Vertical") * walkSpeed * Time.deltaTime;
            Vector3 Movement = mainCamera.right * Horizontal + mainCamera.forward * Vertical;
            Movement.y = 0f;

            controller.Move(Movement);

            // Lass den Player in die Richtung der Camera gucken.
            transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mainCamera.GetComponent<CameraScript>().sensivity * Time.deltaTime);
            Quaternion CamRotation = mainCamera.rotation;
            CamRotation.x = 0f;
            CamRotation.z = 0f;
            transform.rotation = Quaternion.Lerp(transform.rotation, CamRotation, 0.1f);


            // Bekomme den tatsächlichen Playerspeed
            Vector3 horizontalVelocity = controller.velocity;
            horizontalVelocity = new Vector3(controller.velocity.x, 0, controller.velocity.z);
            // The speed on the x-z plane ignoring any speed
            float horizontalSpeed = horizontalVelocity.magnitude;
            // The speed from gravity or jumping
            float verticalSpeed = controller.velocity.y;
            // The overall speed
            float overallSpeed = controller.velocity.magnitude;


            // Gebe dem Animator den OverallSpeed
            animator.SetFloat("currentSpeed", overallSpeed);
        }

        void RemoveFocus()
        {
            focus = null;
        }


        void SetFocus(Interactable newFocus)
        {
            focus = newFocus;
        }


        IEnumerator DoRoll()
        {
            walkSpeed = runSpeed;
            yield return new WaitForSeconds(1);
            animator.SetTrigger("DoRoll");
            yield return new WaitForSeconds(1);
            walkSpeed = 10f;
        }
    }
}