1. 程式人生 > 實用技巧 >Unity 黑魂復刻 - 002

Unity 黑魂復刻 - 002

DSPlayerCtrl.cs


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

public class DSPlayerCtrl : DSMonoBehaviour
{
    public GameObject model;
    //private Animator ani;

    private DSPlayerInput pi;

    private Rigidbody DS_Rigidbody_rigid;

    [SerializeField] private float DS_float_moveSpeed = 10f;

    /// <summary>
    /// 移動向量
    /// </summary>
    private Vector3 movingVec;

    private void Awake()
    {
        //ani = model.GetComponent<Animator>();
        pi = GetComponent<DSPlayerInput>();
        DS_Rigidbody_rigid = DSGetCmpt<Rigidbody>();

        DSEntity.Mono.DSAddUpdateListener(OnUpdate);
        DSEntity.Mono.DSAddFixedUpdateListener(OnFixedUpdate);
    }

    private void OnUpdate()
    {
        //ani.SetFloat("forward", pi.Dmag);
        if (pi.Dmag > 0.1f)
        {
            model.transform.forward = Vector3.Slerp(model.transform.forward, pi.Dvec, 0.3f); //緩慢轉向目標
        }

        DS_float_moveSpeed = pi.run ? 10f : 5f;
        movingVec = pi.Dmag * model.Forward() * DS_float_moveSpeed;
    }

    private void OnFixedUpdate()
    {
        // DS_Rigidbody_rigid.position += movingVec * Time.fixedDeltaTime;
        DS_Rigidbody_rigid.velocity = new Vector3(movingVec.x, DS_Rigidbody_rigid.velocity.y, movingVec.z);
    }
}

DSPlayerInput.cs


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

public class DSPlayerInput : DSMonoBehaviour
{
    public KeyCode keyUp = KeyCode.W;
    public KeyCode keyDown = KeyCode.S;
    public KeyCode keyLeft = KeyCode.A;
    public KeyCode keyRight = KeyCode.D;

    public KeyCode keyA = KeyCode.LeftShift;
    public KeyCode keyB = KeyCode.None;
    public KeyCode keyC = KeyCode.None;
    public KeyCode keyD = KeyCode.None;

    #region 設定方向

    /// <summary>
    /// 向上還是向下,1=上,0=下
    /// </summary>
    public float Dup;

    private float Dup2;

    /// <summary>
    /// 向還是向右,0=左,1=右
    /// </summary>
    public float Dright;

    private float Dright2;

    private Vector2 tempAxis;

    /// <summary>
    /// 輸入開關
    /// </summary>
    public bool inputEnable = true;

    /// <summary>
    /// 角色動畫
    /// </summary>
    [HideInInspector] public float Dmag;

    /// <summary>
    /// 角色方向
    /// </summary>
    [HideInInspector] public Vector3 Dvec;

    //目標方向
    private float targetDup;
    private float targetDright;

    //轉動速度
    private float DupVelocity;
    private float DrightVelocity;


    public bool run = false;

    #endregion

    private void Start()
    {
        DSEntity.Mono.DSAddUpdateListener(OnUpdate);
    }

    private void OnUpdate()
    {
        //設定方向
        targetDup = ((Input.GetKey(keyUp) ? 1.0f : 0f) - (Input.GetKey(keyDown) ? 1.0f : 0f));
        targetDright = ((Input.GetKey(keyRight) ? 1.0f : 0f) - (Input.GetKey(keyLeft) ? 1.0f : 0f));

        if (inputEnable == false) return;

        Dup = Mathf.SmoothDamp(Dup, targetDup, ref DupVelocity, 0.1f);
        Dright = Mathf.SmoothDamp(Dright, targetDright, ref DrightVelocity, 0.1f);

        tempAxis = SquareToCircle(new Vector2(Dup, Dright));

        Dup2 = tempAxis.x;
        Dright2 = tempAxis.y;

        Dmag = Mathf.Sqrt(Dup2 * Dup2 + Dright2 * Dright2);
        Dvec = Dright2 * transform.right + Dup2 * transform.forward;

        run = Input.GetKey(keyA);
    }

    /// <summary>
    /// 正方形轉為橢圓
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    private Vector2 SquareToCircle(Vector2 input)
    {
        Vector2 output = Vector2.zero;

        output.x = input.x * Mathf.Sqrt(1 - (input.y * input.y) / 2.0f);
        output.y = input.y * Mathf.Sqrt(1 - (input.x * input.x) / 2.0f);

        return output;
    }
}