You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity 2D中Distance Joint钩索系统玩家摆动过慢问题

问题:钩索摆动速度过慢与PlayerMovement脚本冲突
  • 在2D平台游戏中使用Distance Joint实现钩索系统,预期玩家钩住物体后能快速摆动,但实际摆动速度过慢
  • 创建了一个仅缺少PlayerMovement脚本的测试Sprite,添加相同钩索脚本后可正常快速摆动;移除玩家身上的PlayerMovement脚本后,玩家也能正常快速摆动,但该脚本是核心功能无法舍弃
  • 尝试用AddForce替代Velocity实现移动时,玩家摆动恢复正常,但角色移动会出现类似冰面打滑的效果

问题相关代码(PlayerMovement脚本)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class PlayerMovement : MonoBehaviour
{
    // Status
    [Header("Status")]
    [SerializeField] public float speed;
    [SerializeField] public float regularSpeed;
    [SerializeField] private float jumpForce;

    // Movement Mechanics
    [Header("Movement Mechanics")]
    public Rigidbody2D rb;
    private float move;
    [SerializeField] private bool isOnGround;
    [SerializeField] private Transform groundCheck;
    [SerializeField] private LayerMask groundLayer;

    [SerializeField] private bool isMovingLeft;
    [SerializeField] private bool isMovingRight;

    public bool canMove;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        speed = regularSpeed;
    }

    void Update()
    {
        // Check for keyboard inputs
        if (Input.GetKey(KeyCode.A))
        {
            MoveLeft();
        }
        else if (Input.GetKey(KeyCode.D))
        {
            MoveRight();
        }
        else if (!isMovingLeft && !isMovingRight)
        {
            StopMoving();
        }

        // Set direction based on button states
        if (isMovingLeft)
        {
            MoveLeft();
        }
        else if (isMovingRight)
        {
            MoveRight();
        }

        // Jump when the Jump button is pressed and player is grounded
        if (Input.GetButtonDown("Jump") && isGrounded() && canMove)
        {
            rb.AddForce(new Vector2(rb.velocity.x, jumpForce));
        }

        if (Input.GetButtonUp("Jump") && rb.velocity.y > 10)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }
    }

    void FixedUpdate()
    {
        // Update Rigidbody velocity based on move direction
        rb.velocity = new Vector3(speed * move, rb.velocity.y, 0);
    }

    // Button press handlers
    public void OnLeftButtonDown() => isMovingLeft = true;
    public void OnLeftButtonUp() => isMovingLeft = false;
    public void OnRightButtonDown() => isMovingRight = true;
    public void OnRightButtonUp() => isMovingRight = false;

    public void MoveLeft()
    {
        transform.eulerAngles = new Vector3(0, 180, 0);
        move = -1;
    }

    public void MoveRight()
    {
        transform.eulerAngles = new Vector3(0, 0, 0);
        move = 1;
    }

    public void Jump()
    {
        if (isGrounded() && canMove)
        {
            rb.AddForce(new Vector2(rb.velocity.x, jumpForce));
        }
    }

    public void StopMoving()
    {
        move = 0;
    }

    private bool isGrounded()
    {
        return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    }
}

效果对比

  • 玩家摆动效果:摆动速度明显偏慢
  • 测试Sprite摆动效果:摆动速度符合预期,快速且正常

内容的提问来源于stack exchange,提问作者ElisCap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 03:24:51