C#中的比率(单位)
我试图应用比率来移动物理库中的对象 我通过测量鼠标滑动的速度,以及第一次单击和松开鼠标时的位置,编程了一种滑动功能 我想在二维空间中,根据你所扫过的角度施加力。我已经尝试使用Mathf.Atan2以及Vector2.angle方法来查找您所扫过的角度,但没有效果 我在想也许我可以看看x和y的运动,然后沿着x和y轴施加适当的力分布 例如:你移动的x是y的一半。我们将把速度变量1/3在x方向上移动,2/3的力在y方向上移动 有没有更简单的方法将这种“滑动”样式的输入应用到运动中C#中的比率(单位),c#,unity3d,physics,angle,C#,Unity3d,Physics,Angle,我试图应用比率来移动物理库中的对象 我通过测量鼠标滑动的速度,以及第一次单击和松开鼠标时的位置,编程了一种滑动功能 我想在二维空间中,根据你所扫过的角度施加力。我已经尝试使用Mathf.Atan2以及Vector2.angle方法来查找您所扫过的角度,但没有效果 我在想也许我可以看看x和y的运动,然后沿着x和y轴施加适当的力分布 例如:你移动的x是y的一半。我们将把速度变量1/3在x方向上移动,2/3的力在y方向上移动 有没有更简单的方法将这种“滑动”样式的输入应用到运动中 using Unit
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(起始位置和结束位置之间的差异)乘以速度,但如果滑动时间较长,则用力会大得多。短而快的划水应该和长划水一样好。也许我可以乘/轮来找到某种解决方案,小的和大的滑动都会产生正确的力量?你只需要用鼠标滑动并使其正常化。然后,您可以将其乘以您选择的恒定速度。因此,要澄清的是,无论滑动的速度或长度有多快,您所需要的只是角度?所以,如果有人在某个方向上快速地长划,那么如果他们在同一方向上缓慢地短划,同样的力也会施加在这个方向上?