<repo_name>1075980722/DormitoryDemo<file_sep>/Assets/Scripts/Player/PlayerMove.cs
﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour
{
    // 移动速度
    public float walkSpeed = 1f;
    // 奔跑速度
    public float runSpeed = 1.5f;
    // 是否使用重力
    public bool useGravity;
    public float gravity = 9.8f;

    // 相机的transform
    private Transform cameraTransform;
    private float moveSpeed;
    private CharacterController controller;
    private float gravitySpeed = 0;
    public bool canMove = true;
    void Start()
    {
        moveSpeed = walkSpeed;
        // 获取相机
        foreach(Transform transform in GetComponentsInChildren<Transform>())
        {
            if(transform.name =="Main Camera")
            {
                Debug.Log("Find Camera!");
                cameraTransform = transform;
            }
        }
        if (!cameraTransform)
        {
            Debug.LogError("Can not Find Camera in Children");
        }
        controller = this.GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        // 如果不能移动
        if (!canMove)
            return;

        // 获取y轴旋转角度
        float yRotation = cameraTransform.rotation.eulerAngles.y;
        // 角度转弧度
        yRotation = yRotation * Mathf.Deg2Rad;
        // cos值
        float cos = Mathf.Cos(yRotation);
        // sin 值
        float sin = Mathf.Sin(yRotation);
        // 向右移动
        moveRight(sin, cos);
        moveFoward(sin, cos);
        SpeedUp();
        // 如果使用重力
        if (useGravity)
        {
            SetGravity();
        }
    }

    // 向右移动
    void moveRight(float sin, float cos)
    {
        float horizontalValue = Input.GetAxis("Horizontal") * moveSpeed;
        Vector3 moveDirection = horizontalValue * (-Vector3.forward * sin+Vector3.right * cos);
        controller.Move(moveDirection * moveSpeed*Time.deltaTime);
    }

    // 向前移动
    void moveFoward(float sin, float cos)
    {
        float verticalValue = Input.GetAxis("Vertical") * moveSpeed;
        Vector3 moveDirection = verticalValue * (Vector3.forward * cos + Vector3.right * sin);
        controller.Move(moveDirection* moveSpeed * Time.deltaTime);
    }

    //加速
    void SpeedUp()
    {
        float speed = Input.GetAxis("SpeedUp");
        // 如果启动加速键
        if (speed == 1f)
        {
            moveSpeed = runSpeed;
        }
        else
        {
            moveSpeed = walkSpeed;
        }
    }
    //设置重力
    void SetGravity()
    {
        if (controller.isGrounded)
        {
            gravitySpeed = 0;
        }
        gravitySpeed -= (gravity * Time.deltaTime);
        Vector3 move = Vector3.zero;
        move.y += gravitySpeed;
        controller.Move(move * Time.deltaTime);
    }
}
<file_sep>/Assets/Scripts/InteractObj/InteractObj.cs
﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 交互类型
public enum InteractObjType
{
    Reapetdly = 0, Once = 1, Twice = 2, Pickup = 3
}

public class InteractObj : MonoBehaviour
{
    public InteractObjType type = InteractObjType.Twice;
    public bool canInteract;
    public bool shouldDestroy;
    public float interactTime = 0.5f;
    private bool isFirstInteraction = true;

    //交互时间是否被重写
    public bool isTimeOverrided = false;
    private PickupObj pickupObj;
    Animator animator;

    void Start()
    {
        if (GetComponent<Animator>())
        {
            animator = GetComponent<Animator>();
        }
        if (GetComponent<PickupObj>())
        {
            type = InteractObjType.Pickup;
            pickupObj = GetComponent<PickupObj>();
        }
        if (GetComponent<Outline>())
            GetComponent<Outline>().SetOutline(false);

    }

    void OnEnable()
    {
        Debug.Log("Interact Obj on enable:" + gameObject.name);
    }
    public void Interact()
    {
        // 如果当前不可交互
        if (!canInteract)
            return;
        canInteract = false;
        switch (type)
        {
            // 两次交互
            case InteractObjType.Twice:
                InteractTwice();
                break;
            case InteractObjType.Pickup:
                Pickup();
                break;
        }

        // 延时执行
        if(!isTimeOverrided)
            StartCoroutine(InteractDown());
    }
    public void ReadyToInteract()
    {
        Debug.Log("readyfjsdl;fj");
        // outline的宽度
        if (GetComponent<Outline>())
            GetComponent<Outline>().SetOutline(true);
    }
    public void CancelInteract()
    {
        if (GetComponent<Outline>())
            GetComponent<Outline>().SetOutline(false);
    }

    IEnumerator InteractDown()
    {
        yield return new WaitForSeconds(interactTime);
        canInteract = true;
    }

    private void InteractTwice()
    {
        Debug.Log("Interact " + this.name);
        if (isFirstInteraction)
        {
            isFirstInteraction = false;
            animator.SetBool("Interaction", true);
        }
        else
        {
            isFirstInteraction = true;
            animator.SetBool("Interaction", false);
        }
    }

    // 捡起
    private void Pickup()
    {
        Debug.Log("Pickup");
        if (isFirstInteraction)
        {
            CancelInteract();
            isFirstInteraction = false;
            pickupObj.Pickup();
        }
        else
        {
            ReadyToInteract();
            pickupObj.PickDown();
            isFirstInteraction = true;
        }
    }

    public bool IsFirstInteraction()
    {
        return isFirstInteraction;
    }
}
<file_sep>/README.md
﻿# unity第一个合作项目 寝室Demo

### 文件结构：
scripts里面放的是代码，Animation是动画，shaders是着色器
