Unity2D 运动 - 速度无法正常工作

问题描述 投票:0回答:1

我已经为此工作了一段时间,但不知道为什么它不起作用。我希望在释放鼠标按钮时,rigidbody2D 对象增加速度,并且按住鼠标的时间长短决定速度的强度。

它在前几次推送中工作正常,但当我开始多次执行时,它似乎记住了之前的速度(即使我将其重置为 0)。有人可以帮忙吗?下面的代码和示例

代码:


public class PlayerControl : MonoBehaviour
{
    public UnityEngine.UI.Slider powerBar;

    private Rigidbody2D rb;
    private float holdDownStartTime;
    private float force;
    private bool moving = false;

    private Vector2 mousePosition;
    private Vector2 targetPosition;
    private Vector3 growthSize = new Vector3(0.1f,0.1f,0);
    

    private void Start()
    {
        powerBar.gameObject.SetActive(false);
        RectTransform rectTransform = powerBar.GetComponent<RectTransform>();
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    { 
        if (Input.GetMouseButtonDown(0))
        {
            //reset RigidBody
            rb.velocity = Vector3.zero;
            rb.angularVelocity = 0;
            rb.position = transform.position;
            rb.isKinematic = true;
            //rb.rotation = transform.rotation;

            moving = false;
            holdDownStartTime = Time.time;
            powerBar.gameObject.SetActive(true);

        }

        if (Input.GetMouseButton(0))
        {
            if (!moving)
            {
                powerBar.value = Time.time - holdDownStartTime;
            }
        }

        if (Input.GetMouseButtonUp(0))
        {

            powerBar.gameObject.SetActive(false);
            float holdDownTime = Time.time - holdDownStartTime;
            force = CalculateHoldDownForce(holdDownTime);
            Move(force);
        }

        mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        RotateTowards(mousePosition);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Food")
        {
            Eat();
        }
    }

    private void Eat()
    {
        this.transform.localScale += growthSize;
    }

    private void Move(float force)
    { 
        targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        rb.velocity = new Vector2(targetPosition.x * force/100, targetPosition.y * force/100);

       
    }

    private void RotateTowards(Vector2 target)
    {
        Vector2 direction = (target - (Vector2)transform.position).normalized;
        var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        var offset = 90f;
        transform.rotation = Quaternion.Euler(Vector3.forward * (angle + offset));
    }

    private float CalculateHoldDownForce(float holdTime)
    {
        holdTime *= 3;
        float maxForceHoldTime = 2f;
        float holdTimeNormalized = Mathf.Clamp01(holdTime / maxForceHoldTime);
        float force = holdTimeNormalized * 100f;
        return force; 
    }

视频示例: https://www.loom.com/share/965cc423e13e4c19a9295f59c5fa896a?sid=8d6e30b2-7f99-4204-a3da-5a720067f292

c# unity-game-engine 2d topdown
1个回答
1
投票

问题在于 Move() 函数中的计算。

private void Move(float force)
{ 
    targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    rb.velocity = new Vector2(targetPosition.x * force/100, targetPosition.y * force/100);
}

在此代码中,您将 targetPosition 向量作为速度输入。正如您在视频中看到的,对象根据中心穿过您的鼠标位置。你应该像这样编辑这个函数:

private void Move(float force)
{ 
    targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    var targetDirection = targetPosition - rb.transform.position;

    rb.velocity = new Vector2(targetDirection.x * force/100, targetDirection.y * force/100);
}
© www.soinside.com 2019 - 2024. All rights reserved.