在 Javascript 中创建萤火虫?

发布于 2024-08-04 18:02:39 字数 343 浏览 5 评论 0原文

我想在 Unity 3D 游戏中创建一些随机飞行的物体。你可以把它们想象成萤火虫。现在需要对它们执行一些任务,包括:

  • 它们通常随机飞行(当然)。
  • 在某些条件下,它们可以飞走或飞回来。

我必须编写一些 Javascript 来完成这些事情,但我不知道该怎么做。你能帮助我吗?非常感谢!

编辑:我想我需要这些功能:

function Fly()
{
   //control the gameObject to fly randomly
}

function FlyAway()
{

}

function FlyBack()
{

}

I want to create some randomly flying objects in a Unity 3D game. You can imagine them like fireflies. Now there're some tasks to do with them, include:

  • They usually fly randomly (of course).
  • They can fly away or fly back in some conditions.

I have to write some Javascript to do these things, but I don't know how to do. Can you help me? Thank you very much!

EDIT: I think I'll need these functions:

function Fly()
{
   //control the gameObject to fly randomly
}

function FlyAway()
{

}

function FlyBack()
{

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗喜 2024-08-11 18:02:39

我建议您查看 canvas 标签,但它不适用于 IE,因为我不知道 3D 效果是否适用于 excanvas

这为您提供了一个可绘制的表面来绘制图形。

您可能需要为图形编写自己的原始函数,但这是非常可行的。

编辑:您需要使用 面向对象的 javascript你的每一个萤火虫,这将帮助你每帧移动它们。

如果您展示了绘制图形的尝试,您可以获得更多帮助。

I would suggest that you look at the canvas tag, but then it will not work on IE as I don't know if 3D effects will work on excanvas well.

This gives you a drawable surface to do your graphics.

You will probably need to write your own primitive functions for the graphics, but it is very doable.

EDIT: You will need to use object oriented javascript for each of your fireflies, which will help you to move each of them each frame.

If you show your attempt at drawing graphics you can get more help.

梦罢 2024-08-11 18:02:39

尽管我讨厌纵容使用 Flash,但这听起来确实像是 Flash 的工作。

但是,如果您坚持使用 JavaScript,我会浏览一些 Chrome 实验的 3D 示例(带画布): http ://www.chromeexperiments.com/

As much as I hate condoning the use of Flash, this really sounds like a Flash job.

However, if you insist on using JavaScript, I'd thumb through some Chrome Experiments for 3D examples (w/canvas): http://www.chromeexperiments.com/

夜灵血窟げ 2024-08-11 18:02:39

我已经完成了一个抽象库,可以在这里提供帮助:

DP_PanelManager< /a>

该库使移动对象变得非常简单 - 在页面底部有一个名为“无用动画”的示例 - 它基本上是一个棋盘,其中每个方块随机移动,改变大小并改变不透明度。

类似这样的东西,用更少的面板和一点数学来实现更顺畅的旅行(类似于 this 应该很容易修改)应该会让你接近。

该组件还具有碰撞检测和面板之间距离的简化功能(带有方位 - 该功能将告诉您两个面板相距多远以及它们放置的方向)。

希望这能让您抢占先机。

I've done an abstraction library that may help here:

DP_PanelManager

The library makes moving objects pretty simple - at the bottom of the page there's an example called "Useless Animation" - it's basically a checkerboard where each square randomly moves, changes size and changes opacity.

Something like that with fewer panels and a little math to do smoother travel (something like this should be easy enough to modify) should get you close.

The component also has simplified functions for collision detection and distance between panels (with bearing - the function will tell you how far apart two panels are AND in which direction they lay).

Hopes this gives you a headstart.

夜巴黎 2024-08-11 18:02:39

我的方法是

  1. 随机创建 ax/y 方程(可能来自给定的集合,如果您不知道如何 100% 随机化)。
  2. 随机决定是按 x 还是按 y。
  3. 决定您在步骤 2 中选择的轴上采取的步数。
  4. 根据从步骤 1 中的方程得到的 x/y 移动元素。
  5. 完成移动所有步数后(步数在步骤中给出) 3)、回到步骤1。循环进行。

My approach would be

  1. randomly create a x/y equation (may be from a given set, if you don't know how to 100% randomize this).
  2. Decide, randomly if you go by the x or by the y.
  3. decide the number of steps you take on the axis you chose in step 2.
  4. move your element according to the x/y you get from the equation in step 1.
  5. When you finished to move all the steps (number of steps is given in step 3), go back to step 1. In a loop.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文