如何在10分钟内做一个弹球游戏?
实战如何在10分钟内做一个弹球游戏?
教程网址:https://developer.unity.cn/projects/61564b34edbc2a001f1ebb88
Visual Studio community 安装报错: VisualStudio.Community.Msi 1316
报错日志如下:
未能安装包“Microsoft.VisualStudio.Community.Msi,version=16.9.31004.235”。
搜索 URL
https://aka.ms/VSSetupErrorReports?q=PackageId=Microsoft.VisualStudio.Community.Msi;PackageAction=Install;ReturnCode=1316
详细信息
MSI: C:\ProgramData\Microsoft\VisualStudio\Packages\Microsoft.VisualStudio.Community.Msi,version=16.9.31004.235\Microsoft.VisualStudio.Community.Msi.msi,属性: REBOOT=ReallySuppress ARPSYSTEMCOMPONENT=1 MSIFASTINSTALL=“7” VSEXTUI=“1” PIDKEY=“NGKBDRWKQFTT82MTRMPKRM6XM” VS7.3643236F_FC70_11D3_A536_0090278A1BB8=“C:\Program Files (x86)\Microsoft Visual Studio\2019\Community” FEEDBACKOPTIN=“1”
返回代码: 1603
返回代码详细信息: 安装时发生严重错误
消息 ID: 1316
消息详细信息: 指定的帐户已存在。
修复工具:https://support.microsoft.com/en-us/topic/fix-problems-that-block-programs-from-being-installed-or-removed-cca7d1b6-65a9-3d98-426b-e9f927e1eb4d
解决步骤
- 下载 troubleshooter
- 运行下载的 MicrosoftProgram_Install_and_Uninstall.meta.diagcab
- 点击安装(表示在安装时出现的问题)
- 选择程序列表里的 vs_communitymsi
- 选择卸载
- 退出 疑难解答程序
- 运行 Visual Studio 安装程序,点击修复
Player 脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// 定义对象移动速度
public float moveSpeed = 5f;
//发射速度
public float shootForce = 5f;
public bool isShoot = false;
//游戏对象 ball
public GameObject ball;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//获取水平输入轴的数值。
float movement = Input.GetAxisRaw("Horizontal");
// 移动方向水平 将 moveSpeed 变量乘以 Time.deltaTime 可确保游戏对象在每一帧匀速移动
transform.Translate(movement * moveSpeed * Time.deltaTime, 0, 0);
if (!isShoot)
{
// 发球功能
if (Input.GetKeyDown(KeyCode.Space))
{ // ball 2d刚体组件 施加力 垂直向上* 速度,ForceMode2D.Impulse瞬时力冲击
ball.GetComponent<Rigidbody2D>().AddForce(Vector2.up * shootForce, ForceMode2D.Impulse);
isShoot = true;
// 摧毁物体 保留子集
transform.DetachChildren();
}
}
}
}
Ball 脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ball : MonoBehaviour
{
//
Transform player;
//创建刚体 rb
Rigidbody2D rb;
//创建Player类对象
public Player playerScript;
//创建计数变量
int score;
public Text scoreText;
// Start is called before the first frame update
void Start()
{
// 初始化
player = transform.parent;
rb = GetComponent<Rigidbody2D>();
score = 0;
}
// Update is called once per frame
void Update()
{ // 转字符串
scoreText.text = score.ToString();
}
// 碰撞时进入方法
private void OnCollisionEnter2D(Collision2D collision)
{ // 碰撞Block
if (collision.gameObject.CompareTag("Block"))
{
// 摧毁Block 元素对象
Destroy(collision.gameObject);
//得分+5
score += 5;
}
}
// 输入时进入方法
private void OnTriggerEnter2D(Collider2D collision)
{ //进入游戏
if (collision.CompareTag("Finish"))
{
transform.SetParent(player);
transform.position = player.position;
rb.velocity = Vector2.zero;
playerScript.isShoot = false;
}
}
}