游戏多人同屏控制器

作者:追风剑情 发布于:2022-6-14 13:13 分类:C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 同屏镜像玩家移动控制器
/// </summary>
public class ActorMoveController : MonoBehaviour
{
    //是否激活状态
    public bool isActive = false;
    //加速度
    public float acceleration = 0.1f;
    //最大速度值
    public float maxSpeed = 1;
    //当前速度值
    public float speed = 0f;
    //方向矢量
    public Vector3 direction = Vector3.forward;
    
    //移动状态
    public MoveState state;

    //移动状态
    public enum MoveState
    {
        //静止
        Static,
        //加速
        Accelerate,
        //匀速
        Constant,
        //减速
        Decelerate
    }

    private Transform cacheTransform;

    private void Awake()
    {
        cacheTransform = transform;
    }

    void Update()
    {
        if (!isActive)
            return;
        Vector3 nextPosition = cacheTransform.position;
        switch(state)
        {
            case MoveState.Accelerate:
                nextPosition = AccelerateMove();
                if (Mathf.Approximately(speed, maxSpeed))
                    state = MoveState.Constant;
                break;
            case MoveState.Constant:
                nextPosition = ConstantMove();
                break;
            case MoveState.Decelerate:
                nextPosition = DecelerateMove();
                if (Mathf.Approximately(speed, 0))
                    state = MoveState.Static;
                break;
        }
        cacheTransform.position = nextPosition;
    }

    // 加速移动
    private Vector3 AccelerateMove()
    {
        speed = Mathf.Min(speed, maxSpeed);
        if (Mathf.Approximately(speed, maxSpeed))
            return cacheTransform.position;

        float a = speed < maxSpeed ? acceleration : 0;
        float t = Time.deltaTime;
        //初速度
        float v0 = speed;
        //末速度
        float vt = v0 + a * t;
        //位移
        float s = v0 * t + 0.5f * a * t * t;
        //重新设置当前速度值
        speed = Mathf.Min(vt, maxSpeed);
        //Next Position
        Vector3 nextPosition = cacheTransform.position + direction * s;
        return nextPosition;
    }

    // 减速移动
    private Vector3 DecelerateMove()
    {
        speed = Mathf.Max(speed, 0);
        if (Mathf.Approximately(speed, 0))
            return cacheTransform.position;

        float t = Time.deltaTime;
        //加速度
        float a = -acceleration;
        //初速度
        float v0 = speed;
        //末速度
        float vt = v0 + a * t;
        //位移
        float s = v0 * t + 0.5f * a * t * t;
        //重新设置当前速度值
        speed = Mathf.Max(vt, 0);
        //Next Position
        Vector3 nextPosition = cacheTransform.position + direction * s;
        return nextPosition;
    }

    // 匀速移动
    private Vector3 ConstantMove()
    {
        float s = speed * Time.deltaTime;
        //Next Position
        Vector3 nextPosition = cacheTransform.position + direction * s;
        return nextPosition;
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号