**
用Unity 3D做简单的吃金币游戏步骤如下
**
1、做出大致场景
2、金币旋转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class xuanzhuan : MonoBehaviour
{
public float xz;
// Start is called before the first frame update
void Start()
{
xz = 120.0f; //旋转速度即角速度
}
// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(0, 1, 0) * xz * Time.deltaTime); //绕Y轴旋转
}
}
3、小球运动利用重力系统
1、先添加重力系统在属性栏的 Add Componet 添加
2、代码实现小球运动和碰到金币消失
下面展示一些 内联代码片
。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class frist : MonoBehaviour
{
public Rigidbody rg;
public float sudu;
// Start is called before the first frame update
void Start()
{
sudu = 20.0f; //小球速度
rg = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// 运动和键盘绑定前后左右跳(W,S,A,D,Speca)
if (Input.GetKey(KeyCode.W))
{
rg.AddForce(Vector3.forward * Time.deltaTime * sudu);
//transform.Translate(Vector3.forward * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.A))
{
rg.AddForce(Vector3.left * Time.deltaTime * sudu);
//transform.Translate(Vector3.left * Time.deltaTime * sudu);
}
if (Input.GetKey(KeyCode.S))
{
rg.AddForce(Vector3.back * Time.deltaTime * sudu);
//transform.Translate(Vector3.back * Time.deltaTime * sudu);
}
if (Input.Ge