2019.12.15 简单unity项目roll a ball
player和UI代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public UnityEngine.UI.Text tips;
Rigidbody rigid;//引用刚体组件
int count;
float speed = 3;
void Start()
{
rigid = GetComponent<Rigidbody>();// 获取刚体组件, GetComponent< >()是泛型函数句子
}
void Update()
{
//取值范围
// 1
//-1 1
// -1
var h= Input.GetAxis("Horizontal");//获取水平轴输入(用于玩家控制方向),放在变量h中
var v= Input.GetAxis("Vertical");//获取垂直轴输入
var movement = new Vector3(h,0,v) ;//创建vector3变量movement 把变量h,v,赋给x,z轴
rigid.AddForce(movement*speed);//添加一个力到刚体。作为结果刚体将开始移动。
}
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<Gold >())//如果other的碰撞被检测到?
{
other.gameObject.SetActive(false );//用来将白色方块的碰撞后可见性设为无
count++;
RefreshTips();
}
}
void RefreshTips()
{
tips.text = "Count:" + count.ToString();
// 用来显示吃到的白色方块个数,这里的count.ToString()不太理解,数字字符串化?
if (count >= 7)
{
tips.text = "win";
}
}
//这个函数用于刷新得分显示
}
Gold(游戏中的白色方块)代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gold : MonoBehaviour
{
void Start()
{
}
void Update()
{
transform.Rotate(new Vector3(30, 30, 30)*Time.deltaTime);
//让物体每秒旋转固定的角度,deltaTime(增量时间 当前帧时间减去上一帧时间的差值,
//使游戏世界每一秒的变化都是稳定的,不受帧率波动的影响)
}
}
暂时不太理解的代码
1.if (other.GetComponent<Gold >())//如果other的碰撞被检测到?
2.other.gameObject.SetActive(false );//用来将白色方块的碰撞后可见性设为无?
3. tips.text = "Count:" + count.ToString();
// 用来显示吃到的白色方块个数,这里的count.ToString()不太理解,数字字符串化?
来源:CSDN
作者:狂徒煮青蛙
链接:https://blog.csdn.net/weixin_44174876/article/details/103571061