unity3d 打砖块操作及脚本

unity3d 打砖块操作及脚本
unity3d 打砖块操作及脚本

camera clear flag 改为solid color 固定的颜色

设置background

size =125 使游戏视图高度为125*2

图片format设为truecolor

图片属性可以设定layer层级

给bat添加rigid 2d 刚体

勾选freeze rotation 冻结z轴旋转,从而不让bat旋转出平面,不在z轴旋转

freeze position y使bat不能在y轴移动

gravity scale=0

FixedUpdate会在每个固定的时间间隔被调用,

那么要是Update 和FixedUpdate的时间间隔一样,是不是就一样呢?答案是不一定,因为Update受当前渲染的物体,更确切的说是三角形的数量影响,有时快有时慢,帧率会变化,update被调用的时间间隔就发生变化。但是

FixedUpdate则不受帧率的变化,它是以固定的时间间隔来被调用,那么这个时间间隔怎么设置呢?Edit->Project Setting->time下面的Fixed timestep。

public class bat : MonoBehaviour {

public float speed = 150f;

//FixedUpdate会在每个固定的时间间隔被调用,

void FixedUpdate()

{ //得到用户输入,用输入轴实现平滑输入

float h = Input .GetAxis( "Horizontal");

//得到游戏对象上的刚体组件

//vector2:代表2D向量和点

//.Right相当于0,0点到1,0点的向量

GetComponent< Rigidbody2D >().velocity=Vector2 .right*h*speed;

}

}

给bat和边框加上box colider 2d

注意colider的size是按图片大小来的,最好要贴着框架

还有bat 的colider要贴着轮廓

用offset可以调整还有edit

object sortinglayer add

选择+ 增加层unity会从上往下绘制层

将ball和bat放在下面的层

记住要把背景上的object都放在下面的层,才能避免绘制问题

设置好ball的组件

create physics material 2d

fraction 摩擦系数设为0

bounciness 弹性系数设为1

拖到colider的material

这样小球就具有弹性了,而且无摩擦

设置rigid的mass质量为0.0001

collision detection =continuous

interpolate =interpolate

gravity初始设为0

在ball界面可以更改speed

把ball旋转关了,避免小球速度突然降为0

实现全局变量Enter类中:

public static int blocknum = 20;

其他类中:

Enter.blocknum -= 1;

HP类中:

public static int hp =1;

void Update () {

GetComponent< Text >() .text= ("HP:" +hp);

}

要访问绑定了该script的object的其他属性,打钩组件的属性必须要getcomponent后才能访问

另一种访问属性的方法是先定义gameobject类型或其他类的类型,然后拖入

访问时必须先写组件名(transform)再.属性

如果发现text字没有显示可能是文本框划的不够大

ball中

public float x = 0.2f;

public float y = -90.2f;

void Update()

{

if (transform.position.y<-110)

{

HP .hp -= 1;

Instantiate(Ball, new Vector2 (x, y), Quaternion.identity);

Destroy(gameObject); //不要忘记把原来的对象删掉,不然会fatal }

}

实现球随拍子移动

若出现问题:The object of type 'ball' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

原因:未将使用过的函数destory导致其仍然生效

实现将ball与bat连接:

HingeJoint2D铰链

将发射时的坐标设置为跟着bat的坐标:

void Start () {

Vector3 ballvtr = transform.position;//将ball的向量赋给ballvtr

ballvtr.x = Bat.transform.position.x-1.3f; //小数不能漏f

transform.position = ballvtr;

GetComponent< Rigidbody2D >().velocity = Vector2 .up * speed;

}

附上代码

BALL类

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

public class ball : MonoBehaviour {

public float speed = 100f;

public ball Ball;

public bat Bat;

public Text title;

// Use this for initialization

void Start () {

Vector3 ballvtr = transform.position;//将ball的向量赋给ballvtr

ballvtr.x = Bat.transform.position.x-1.3f; //小数不能漏f

transform.position = ballvtr;

Ball.GetComponent< HingeJoint2D >().enabled = true ;

}

// 框架方法当发生碰撞时调用

void OnCollisionEnter2D( Collision2D col) {//col指撞击的对象

if (https://www.360docs.net/doc/3813979592.html, == "bat" )

{

float x = hitVector(transform.position, col.transform.position,

col.collider.bounds.size.x); //从撞向的对象的碰撞体的size获得其宽度

Vector2 dir = new Vector2(x, 1).normalized; //单位化向量方向

GetComponent< Rigidbody2D >().velocity = dir * speed;//改变方向}

}

float hitVector( Vector2 ballpos,Vector2 batpos, float batwidth)

{ //返回球撞击处占bat宽度的比例,注意要是float

return (ballpos.x - batpos.x) / batwidth;

}

public float x = 0.2f;

public float y = -90.2f;

void Update()

{

if (transform.position.y<-110)

{

HP .hp -= 1;

Instantiate(Ball, new Vector2 (x, y), Quaternion.identity);

Destroy(gameObject); //不要忘记把原来的对象删掉,不然会fatal }

if (Input .GetKeyUp( KeyCode.Space)) //按键要放在UPDATE才会生效

{

Ball.GetComponent< HingeJoint2D >().enabled = false ;

//使铰链失效,但不能删掉

GetComponent< Rigidbody2D >().velocity = Vector2 .up * speed;

}

}

}

相关主题
相关文档
最新文档