【发布时间】:2023-04-04 07:05:01
【问题描述】:
我有一个游戏,在投掷箭头之前绘制路径,但路径与投掷后的箭头路径不匹配。
这是路径的代码:
public void DrawThePath()
{
cm.clear_circles();// cm is object for drawing circle
float x = transform.position.x;
float y;
for (int i=0; i<circleNumbers; i++)
{
y = calculateHeight(x - Arrow.transform.position.x);
cm.draw_circle(x, y, circleRedius);
x += circleXdistance;
if (x > Aim.transform.position.x)
{
break;
}
}
x = Aim.transform.position.x;
y = calculateHeight(x - Arrow.transform.position.x);
cm.draw_circle(x, y, circleRedius);
}
private float calculateHeight(float x)
{
float g = Physics2D.gravity.y;
float theta = Arrow.transform.rotation.z * 2 / 3 * Mathf.PI;
float velocity = FindObjectOfType<HandMove>().getVelocity();
float initialY = Arrow.transform.position.y;
float y = g / 2 * Mathf.Pow(x / (Mathf.Cos(theta) * velocity), 2);
y += Mathf.Tan(theta) * x;
y += initialY;
return y;
}
以及投掷箭的代码:
public void throwArrow()
{
velocity = FindObjectOfType<HandMove>().getVelocity();
GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
GetComponent<Rigidbody2D>().velocity = new Vector2(velocity * Mathf.Cos(Angle), velocity * Mathf.Sin(Angle));
}
【问题讨论】:
-
没有深入研究您的代码,但是如果您使用刚体,则托盘不应是圆形,而是抛物线
-
@rustyBucketBay 圆的轨迹在哪里?您指的是沿路径绘制的圆圈吗?
-
是的,这个函数叫做draw_circle,你传给它一个中心和一个可变半径,这就是为什么我假设你在哪里画一个圆。
-
@rustyBucketBay 是的,之所以这样称呼是因为正在绘制圆圈。可以毫无问题地在抛物线路径上绘制圆心。 draw_circle 没有绘制路径的主要线索是它不需要初始速度
标签: c# unity3d game-physics