老婆出轨 发表于 2025-4-9 16:03:04

在Unity中实现《幽灵行者》风格的跑酷动作

基础设置


[*] 角色控制器选择:

[*] 使用Character Controller组件或Rigidbody + Capsule Collider
[*] 推荐使用Character Controller以获得更精确的运动控制

[*] 输入系统:

[*] 使用Unity的新输入系统(Input System Package)处理玩家输入

滑铲实现

public class SlideController : MonoBehaviour
{
   
    public float slideSpeed = 10f;
    public float slideDuration = 1f;
    public float slideCooldown = 0.5f;
    public float slideHeight = 0.5f;
    public float normalHeight = 2f;
   
    private CharacterController controller;
    private bool isSliding = false;
    private float slideTimer;
    private float cooldownTimer;

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

    void Update()
    {
      HandleSlide();
    }

    void HandleSlide()
    {
      if (cooldownTimer > 0)
      {
            cooldownTimer -= Time.deltaTime;
            return;
      }

      if (Input.GetKeyDown(KeyCode.LeftControl) && !isSliding)
      {
            StartSlide();
      }

      if (isSliding)
      {
            slideTimer -= Time.deltaTime;
            if (slideTimer <= 0)
            {
                EndSlide();
            }
            
            // 保持滑铲速度
            Vector3 moveDirection = transform.forward * slideSpeed;
            controller.Move(moveDirection * Time.deltaTime);
      }
    }

    void StartSlide()
    {
      isSliding = true;
      slideTimer = slideDuration;
      controller.height = slideHeight;
      controller.center = new Vector3(0, slideHeight * 0.5f, 0);
    }

    void EndSlide()
    {
      isSliding = false;
      cooldownTimer = slideCooldown;
      controller.height = normalHeight;
      controller.center = new Vector3(0, normalHeight * 0.5f, 0);
    }
} 贴墙跑实现

public class WallRunController : MonoBehaviour
{
   
    public float wallRunSpeed = 8f;
    public float wallRunGravity = 2f;
    public float wallRunDuration = 2f;
    public float wallJumpForce = 10f;
    public LayerMask wallRunLayer;
   
    private CharacterController controller;
    private bool isWallRunning = false;
    private Vector3 wallNormal;
    private float wallRunTimer;

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

    void Update()
    {
      CheckWallRun();
      HandleWallRun();
    }

    void CheckWallRun()
    {
      if (isWallRunning) return;
      
      RaycastHit hit;
      if (Physics.Raycast(transform.position, transform.right, out hit, 1f, wallRunLayer))
      {
            StartWallRun(hit.normal, false);
      }
      else if (Physics.Raycast(transform.position, -transform.right, out hit, 1f, wallRunLayer))
      {
            StartWallRun(hit.normal, true);
      }
    }

    void StartWallRun(Vector3 normal, bool isLeftWall)
    {
      isWallRunning = true;
      wallNormal = normal;
      wallRunTimer = wallRunDuration;
      
      // 调整角色朝向与墙面平行
      Vector3 cross = Vector3.Cross(normal, Vector3.up);
      transform.rotation = Quaternion.LookRotation(cross, normal);
    }

    void HandleWallRun()
    {
      if (!isWallRunning) return;
      
      wallRunTimer -= Time.deltaTime;
      
      // 沿墙面移动
      Vector3 moveDirection = transform.forward * wallRunSpeed;
      
      // 应用自定义重力
      moveDirection.y -= wallRunGravity * Time.deltaTime;
      
      controller.Move(moveDirection * Time.deltaTime);
      
      // 检查是否应该结束贴墙跑
      if (wallRunTimer <= 0 || !Physics.Raycast(transform.position, wallNormal, 1f, wallRunLayer))
      {
            EndWallRun();
      }
      
      // 墙跳
      if (Input.GetButtonDown("Jump"))
      {
            WallJump();
      }
    }

    void WallJump()
    {
      Vector3 jumpDirection = (wallNormal + Vector3.up).normalized;
      // 应用跳跃力...
      EndWallRun();
    }

    void EndWallRun()
    {
      isWallRunning = false;
    }
} 高级本事


[*] 动画混合:

[*] 使用Animator Controller混合差异动作的动画
[*] 设置得当的过渡条件和混合树

[*] 运动曲线:

[*] 使用AnimationCurve调解动作的速度变化,使过渡更自然

[*] 相机结果:

[*] 在特殊动作时添加相机震动、视野变化等结果
[*] 使用Cinemachine实现平滑的相机跟随

[*] 物理材质:

[*] 为差异表面设置差异的物理材质,影响摩擦力和弹跳结果

[*] 粒子结果:

[*] 在滑铲时添加灰尘粒子
[*] 在贴墙跑时添加墙面火花结果

优化建议


[*] 状态机模式:

[*] 实现一个完整的状态机管理系统,管理跑酷的各种状态
[*] 例如:站立、奔跑、滑铲、贴墙跑、跳跃等状态

[*] 输入缓冲:

[*] 实现输入缓冲系统,使动作衔接更流畅

[*] 物理预测:

[*] 使用射线检测预测即将到来的动作时机(如火线可滑铲地域或可贴墙跑的表面)


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 在Unity中实现《幽灵行者》风格的跑酷动作