controls a page

PHOTO EMBED

Sun Nov 22 2020 21:32:19 GMT+0000 (Coordinated Universal Time)

Saved by @huntercoad

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PageController : MonoBehaviour
{
    CharacterController controller;

    public float speed = 15.0f;
    public float jumpSpeed = 20.0f;
    public float gravity = 6.0f;
    public float life = 1f;

    public GameObject player;
    public GameObject page;

    public PlayerMovement playerScript;

    public bool canFloat = false;

    public Transform exitPoint;

    Vector3 velocity;

    public AudioSource musicSource;
    public AudioClip pageAudio;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        musicSource = GetComponent<AudioSource>();
    }

    void Update()
    {
        if (controller.isGrounded)
        {
            speed = 0;
        }

        if (Input.GetKeyDown(KeyCode.E) && playerScript.inPage == false && life == 1 && playerScript.distToPage < 8f)
        {
            playerScript.inPage = true;
            playerScript.transform.position = exitPoint.position;
            playerScript.transform.rotation = Quaternion.Euler(0, 0, 0);
            playerScript.canMove = false;
            //rend.enabled = false;
        }

        if (playerScript.inPage)
        {
            canFloat = true;
        }

        if (canFloat)
        {
            if (Input.GetKeyDown(KeyCode.Space) && life == 1)
            {
                velocity.y = jumpSpeed;
                Invoke("lifeloss", 1);
                speed = 20;
                musicSource.clip = pageAudio;
                musicSource.Play();
            }

            if (controller.isGrounded && life <= 0)
            {

                playerScript.inPage = false;
                playerScript.canMove = true;
                canFloat = false;
                musicSource.Stop();

            }

            float x = Input.GetAxis("Horizontal");
            float z = Input.GetAxis("Vertical");

            Vector3 move = transform.right * x + transform.forward * z;

            controller.Move(move * speed * Time.deltaTime);

            controller.Move(velocity * Time.deltaTime);

            velocity.y -= gravity * Time.deltaTime;

            page.transform.rotation = player.transform.rotation;
        }

        else
        {
            controller.Move(velocity * Time.deltaTime);
            velocity.y = -6;
        }

    }

    void lifeloss()
    {
        life -= 1;
    }
}
content_copyCOPY