糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > 小球撞击砖块C语言 制作一个小球撞击砖块游戏

小球撞击砖块C语言 制作一个小球撞击砖块游戏

时间:2022-11-28 11:32:54

相关推荐

小球撞击砖块C语言 制作一个小球撞击砖块游戏

1.新建一个Plane

2.新建一个Wall,编辑脚本生成6*8的墙

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Brick : MonoBehaviour {

public GameObject brick;

private int columnNum = 8;

private int rowNum = 6;

// Use this for initialization

void Start () {

for(int i=0;i

{

for(int j=0;j

{

Instantiate(brick, new Vector3(j - 5, i), Quaternion.identity);

}

}

}

// Update is called once per frame

void Update () {

}

}

3.生成一个Sphere球体,相机绑定脚本

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Shoot : MonoBehaviour {

public GameObject shootPos;

private float force = 1000;

public Rigidbody shootBall;

private float speed = 0.1f;

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

Rigidbody ball;

if (Input.GetKeyDown(KeyCode.Space))

{

ball = Instantiate(shootBall,shootPos.transform.position,Quaternion.identity) as Rigidbody;

ball.AddForce(force * ball.transform.forward);

}

if(Input.GetKey(KeyCode.LeftArrow))

{

this.transform.Translate(Vector3.left * speed);

}

else if (Input.GetKey(KeyCode.RightArrow))

{

this.transform.Translate(Vector3.right * speed);

}

else if (Input.GetKey(KeyCode.UpArrow))

{

this.transform.Translate(Vector3.up * speed);

}

else if (Input.GetKey(KeyCode.DownArrow))

{

this.transform.Translate(Vector3.down * speed);

}

}

}

4.发射之后销毁球

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class BallDestroy : MonoBehaviour {

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

Destroy(this.gameObject, 3f);

}

}

如果觉得《小球撞击砖块C语言 制作一个小球撞击砖块游戏》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。