簡體   English   中英

我如何在玩家范圍內將游戲 object 移動到鼠標 position

[英]How can i move game object to mouse position while in players range

我正在嘗試制作球體武器。 我幾乎嘗試了所有方法,但無法在玩家范圍內制作跟隨鼠標的武器。 誰能幫我?

這是播放器層次結構: Player Prefab

這是我的代碼。 這段代碼中的問題是 maxDistance 總是在 0-6 之間。 最大距離不適合玩家移動。 如果您對此有更好的解決方案,那將是一件很棒的事情。

    public Transform weaponParent;
    public float maxMoveSpeed = 10;
    public float smoothTime = 0.3f;
    Vector2 currentVelocity;
    [SerializeField] private float maxDistance = 6f;

    void FixedUpdate()
    {
        Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        var target = Vector2.ClampMagnitude(mousePosition, maxDistance);

        weaponParent.position = Vector2.SmoothDamp(weaponParent.position, target, ref currentVelocity, smoothTime, maxMoveSpeed);

    }

我想我現在明白你的問題了

您正在夾緊 position(這是一個從世界 (0,0,0) 開始的矢量,您更想夾緊它們之間的矢量,例如

var target = transform.position +  Vector2.ClampMagnitude(mousePosition - transform.position, maxDistance);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM