unity 碰撞监测脚本
In this tutorial, I will show you how to create a Ball and wall collision effect.
在本教程中,我将向您展示如何创建“球与墙”碰撞效果。
I’ll use the simple hittest()
function in Flash to achieve this effect. Download the sample files here. Let’s start working.
我将在Flash中使用简单的hittest()
函数来实现此效果。 在此处下载示例文件 。 让我们开始工作。
1. Open a new movie wiht a width=550 and a height=400.
1.打开一部新电影,其宽度= 550和高度= 400。
2. First we will create the outer wall. Create a small rectangle object like this one:
2.首先,我们将创建外墙。 创建一个像这样的小矩形对象:
![1094_wall](https://i-blog.csdnimg.cn/blog_migrate/66f7ecf62c2c2ae0e4cc9b53ec2596be.gif)
3. Convert the rectangle object to a movieclip, and name it "wall".
3.将矩形对象转换为动画片段,并将其命名为“ wall”。
4. Now, drag four instances of the wall object and arrange as below.
4.现在,拖动墙对象的四个实例,并如下进行排列。
![1094_box](https://i-blog.csdnimg.cn/blog_migrate/7d39ce47fd1f3d583d45b6829d267e3b.gif)
5. Give each wall an instance name. I used the instance names border1, border2, border3, and border4.
5.给每面墙一个实例名称。 我使用了实例名称border1,border2,border3和border4。
6. Now let’s create the ball. Create a small circle object.
6.现在,我们来创建球。 创建一个小圆圈对象。
7. Convert it to a movieclip, and give it an instance name. I called it "circle".
7.将其转换为动画片段,并为其指定实例名称。 我称它为“圆圈”。
8. Now we will give the action script. Create three key frames as shown below.
8.现在,我们将提供动作脚本。 创建三个关键帧,如下所示。
![1094_keyframe](https://i-blog.csdnimg.cn/blog_migrate/54805839d72af979d529f0f77fadf174.gif)
9. In the first key frame, instert the action:
9.在第一个关键帧中,插入动作:
flag = 0;
//x coordinate =0
x= 190;
//y cordinate =0
y=190;
//random = 0
rand = 0;
10. Initialize the variable in the first key frame.
10.在第一个关键帧中初始化变量。
11. In the second key frame, insert these actions:
11.在第二个关键帧中,插入以下操作:
//set the x and y property of the circle movieclip
setProperty ("/circle", _x, x);
setProperty ("/circle", _y, y);
// creates a random number with this function
function random_fun()
{
r = random(10);
return r;
}
// check if circle has hit border4 if hit change x and y values
if (circle.hittest(border4))
{
tellTarget ("/circle") {gotoAndPlay(2)}
flag = 1;
rand = random_fun()
}
// check if circle has hit border3 if hit change x and y values
if (circle.hittest(border3))
{
tellTarget ("/circle") {gotoAndPlay(2)}
flag = 2;
rand = random_fun()
}
// check if circle has hit border2 if hit change x and y values
if (circle.hittest(border2))
{
tellTarget ("/circle") {gotoAndPlay(2)}
flag = 3;
rand = random_fun()
}
// check if circle has hit border1 if hit change
x and y values
if (circle.hittest(border1))
{
tellTarget ("/circle") {gotoAndPlay(2)}
flag = 4;
rand = random_fun()
}
// set the x and y co-ordinates according the the
place where the circle hit
if (flag ==0) {
x = x+rand;
y = y+5;
}
if (flag == 1) {
x = x+5;
y = y-rand;
}
if (flag == 2) {
x = x-5;
y = y-rand;
}
if (flag == 3) {
x = x-rand;
y = y+5;
}
if (flag == 4) {
x = x + 5;
y = y - rand;
}
// end action script
12. In the third key frame insert the action:
12.在第三个关键帧中,插入操作:
gotoAndPlay (2);
That’s it! The movie should now work!
而已! 电影现在应该可以了!
This movie is not as complex as it looks. Actually, it’s very easy compared to others found on the Web. There are a lot of possibilities with this movie. Let your imagination run wild!
这部电影并不像看起来那样复杂。 实际上,与在网络上找到的其他工具相比,这非常容易。 这部电影有很多可能性。 让想象力自由驰骋!
unity 碰撞监测脚本