DR
把我的游戏从处理转移到统一。通过手动改变玩家的速度来抓取的代码不起作用,即使它基本上是复制/粘贴的。
嗨,我整个夏天都在做一个处理项目,上周我决定把它翻译成Unity。
我遇到的问题是抓钩/绳索物理。它应该基本上保持玩家在一个圆圈内(由绳子的端点和绳子的长度构成)。当玩家落在此圆之外时,玩家的位置将移回圆的边缘,玩家的速度将设置为圆的正切。
在摇摆的时候减少绳子的长度应该可以加速你。(见
Floating Point
)
在处理过程中,它完全像上面描述的那样工作,但是当我基本上复制/粘贴代码到Unity中时,它会很快失去动力(总是以另一方开始时的相同角度停止)。这是两者的代码(在每个物理帧上运行):
(我还制作了一些图像来描述两个版本产生的运动)
处理
代码
(警告:不良和冗余)
物理更新:
exists = (endPoint != null);
if(lgth<=0) lgth = 1;
if(exists) {
currentLength = phs.position.dist(endPoint);
if(currentLength > lgth) {
float angle = getAngle(endPoint, phs.position);
phs.addPosition(abs(currentLength - lgth), angle);
float angleBetween = getAngle(phs.position, endPoint);
PVector relativeVelocity = new PVector(phs.velocity.x + phs.position.x, phs.velocity.y + phs.position.y);
float displacement = angleBetween - 90;
Line l1 = lineFromTwoPoints(relativeVelocity, endPoint);
Line l2 = lineFromAngle(phs.position, displacement);
PVector pointToLerpTo = intersection(l1, l2);
if(pointToLerpTo!=null) {
phs.velocity.x = pointToLerpTo.x-phs.position.x;
phs.velocity.y = pointToLerpTo.y-phs.position.y;
}
else phs.velocity.mult(0);
}
}
当运动员缩短绳索时,速度增加:
if(exists) {
float newLgth = lgth-d;
float distance = getDistance(phs.position, endPoint);
if(distance > newLgth) {
float ratio = (distance-newLgth)/lgth;
phs.velocity.setMag(phs.velocity.mag()*(1+ratio));
}
lgth = newLgth;
}
处理过程中的运动(良好)
Player starts by moving downwards at left edge of rope circle. Doesn't lose speed and continues going around multiple times until gravity slows it down.
团结
代码
上面的两个代码块在这里和下面的相同位置处理
FixedUpdate()
(有问题的部分似乎是速度部分)
distance = Vector2.Distance(transform.position, endpoint);
if(connected && distance > length) {
//lerp position -> endpoint// keep gameObject within length of the rope
float posLerpAmount = (distance - length) / distance;
transform.position = Vector2.Lerp(transform.position, endpoint, posLerpAmount);
//'lerp' velocity -> endpoint// keep the velocity locked to the tangent of the circle around the endpoint
Vector2 relativeVelocity = GetComponent<Rigidbody2D>().velocity + (Vector2)transform.position;
Line l1 = Geometry.LineFromTwoPoints(relativeVelocity, endpoint);
Line l2 = Geometry.LineFromAngle(transform.position, Geometry.GetAngle(endpoint, transform.position) - 90);
if(!Geometry.AreParallel(l1, l2)) {
Vector2 pointToLerpTo = Geometry.Intersection(l1, l2) - (Vector2)transform.position;
GetComponent<Rigidbody2D>().velocity = pointToLerpTo;
}
else GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
//increases the magnitude of the velocity based on how far the rope moved the object's position
float ratio = (distance - length) / length;
GetComponent<Rigidbody2D>().velocity *= 1 + ratio;
distance = length;
}
团结运动(坏)
Player starts by moving downward at left edge of rope circle. Gains a little bit of speed from gravity, then will always stop 45 degrees on the other side where it started (regardless of starting speed), then slowly fall back down to the bottom of the circle.
如果有人需要我解释几何类(直线,交叉点),那么我可以,但我认为这主要是不言自明的。否则,我想我会尽我所能解释清楚。事先谢谢你的帮助。
(另外,StackOverflow不允许我添加Unity2d标签,所以我想我必须满足于Unity3d)