【问题标题】:Ratios in C# (in Unity)C# 中的比率(在 Unity 中)
【发布时间】:2014-02-28 07:10:56
【问题描述】:

我正在尝试应用比率来移动物理库中的对象。

我通过测量鼠标滑动的速度以及您第一次单击和松开时的位置来编写一种滑动类型的功能。

我想根据您滑动的角度在二维空间中施加力。我尝试使用 Mathf.Atan2 以及 Vector2.angle 方法来查找您滑动的角度,但无济于事。

我在想也许我可以查看 x 和 y 运动,然后从那里沿 x 和 y 轴施加适当的力分布?

例如:您移动的 x 是 y 的一半。所以我们将速度变量 1/3 沿 x 方向移动,2/3 力沿 y 方向移动。

有没有更简单的方法将这种“滑动”样式输入应用到运动中?

using UnityEngine;
using System.Collections;

public class MouseInteraction : MonoBehaviour {
    public int SpeedMultiplier = 2;
    public Vector2 startPos;
    public Vector2 endPos;
    public float startTime;
    public float endTime;
    public float mouseDistance;
    public float mouseTime;
    public float mouseSpeed;
    public float mouseAngle;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetMouseButtonDown (0)) 
                {
                        Vector2 startPos = Camera.main.ScreenToViewportPoint (Input.mousePosition);
                        float startTime = Time.time;
                        Debug.Log ("The start position is " + startPos);
                        Debug.Log ("The start time is " + startTime);
                }
        else if (Input.GetMouseButtonUp (0)) 
                {
                    Vector2 endPos = Camera.main.ScreenToViewportPoint (Input.mousePosition);
                    float endTime = Time.time;
                    Debug.Log ("The end position is " + endPos);
                    Debug.Log ("The end time is " + endTime);
                    float mouseDistance = Vector2.Distance(startPos, endPos);
                    Debug.Log ("The mouse distance is " + mouseDistance);
                    float mouseTime = endTime - startTime;
                    Debug.Log ("The mouse time is " + mouseTime);
                    float mouseSpeed = mouseDistance/ mouseTime;
                    Debug.Log ("The mouse SPEED is " + mouseSpeed);
                    float mouseAngleX = endPos.x - startPos.x;
                    float mouseAngleY = endPos.y - startPos.y;
                    Debug.Log ("The mouseX number is " +mouseAngleX);
                    Debug.Log ("The mouseY number is " +mouseAngleY);

                    }
    }
}

具体来说,我应该补充一点,我现在唯一坚持的是应用正确数量的水平和垂直动量。我可以将每个 mouseAngleX 和 mouseAngleY(开始位置和结束位置之间的差异)乘以 Speed,但是更长的滑动会产生更大的力量。短而快的滑动应该和长的一样好。也许我可以乘法/四舍五入找到某种解决方案,其中大小滑动都会产生正确的力?

【问题讨论】:

  • 所以要澄清一下,无论滑动多快或多长时间,您只需要角度吗?因此,如果某人在某个方向上进行了长时间的快速滑动,那么如果他们在同一方向上进行了一次短而缓慢的滑动,同样的力会施加到它身上吗?

标签: c# unity3d physics angle


【解决方案1】:

您可以只用鼠标滑动并将其标准化。然后,您可以将其乘以您选择的恒定速度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多