我有一个游戏,您可以在平台上弹跳球,您必须摧毁所有盒子,但是我的反弹球的代码不像我想要的那样工作。
它的作用就像真正的球弹跳一样,但我希望球像乒乓球一样弹跳,直到它碰到墙壁或platfotm,但我的球在现实生活曲线中才会掉下来。
我的曲线事物就像当您在空中踢球并进入曲线时,我希望它在平坦的线上沿墙壁无限,并随机方向。
有人有任何代码想法吗?
I have a game where u bounce a ball with platform and u have to destroy all boxes but my code for bounceing ball its not working like I want.
It works like a real ball bounce but I want the ball to bounce like a ball in pong where ball don't fall down until it touches wall or platfotm but my ball goes in real life curve.
What I thing with curve is like when u kick a ball in the air and goes in curve but I want it to go unlimitly in flat line to the wall andbounce in a random direction.
Someone have any code ideas?
发布评论
评论(2)
制作像下面的物理材料,并将其放置在任何弹跳的表面上。它足以让您的球拥有一个
strigbody
带有0。也请取消选中的重力以获得以下结果:
“ https://i.sstatic.net/4a5wi.gif” rel =“ nofollow noreferrer”>
基本力量脚本:
将刚性体添加到球中后,您可以将以下脚本用于初始力量。显然,您需要更高级的算法才能进一步开发。
Make a physics material like the one below and place it on any surface that bounces. It is enough for your ball to have a
Rigidbody
with drag 0.Also uncheck gravity to get the following result:
Basic Force Script:
After adding Rigidbody to the ball, you can use the following script for the initial force. Obviously you need more advanced algorithms for further development.
乒乓球风格的物理学比您想象的要简单。
球没有对它们应用重力,因此您要做的就是在球上施加恒定的速度。
当它与物体碰撞时,您会在其相撞的轴上翻转速度。
例如:
Ball以60px/sec的速度向上行驶,并以120px/sec的水平行驶,以60fps的速度向上行驶。
您将(60/60)= 1个像素垂直添加每个帧,(120/60)= 2水平。
它撞到墙壁,因此您将水平速度翻转为-120px/sec。
现在它仍在上升,但水平方向相反。
如果碰到天花板,则可以将垂直速度组件翻转。
如果您想朝随机方向发展:
sqrt(vx^2+vy^2)
不要在每个帧中减去/添加恒定值,因为这样您就可以应用重力,这不是您想要的。
Pong/breakout style physics is simpler than you might think.
The balls have no gravity applied to them, so all you need to do is apply a constant velocity to the ball.
When it collides with an object, you would flip the velocity on the axis it collided.
For example:
ball is travelling up at 60px/sec, and horizontally at 120px/sec, at 60fps.
You add (60/60)=1 pixels vertically every frame, and (120/60)=2 horizontally.
It hits a wall, so you flip the horizontal velocity to -120px/sec.
Now it's still going up, but in the opposite direction horizontally.
If it hits a ceiling, same thing but flip the vertical velocity component.
If you'd rather it go in a random direction:
sqrt(vx^2+vy^2)
Don't subtract/add a constant value to the velocity every frame, because then you'd be applying gravity, which isn't what you're looking for.