Unity3D官方推荐教程roll a bal案例
首先声明本人菜鸟一枚,此博客只是记录一下学习过程,虽说是跟着教程一步一步做的,但是在做的过程当中也遇到了很多问题(菜是原罪),这些问题精良写在了文中,仅供参考!大神们手下留情,不喜勿喷,谢谢!
1.游戏界面
游戏界面和台球桌类似,游戏对象也十分简单,这里就不做赘述。此游戏主要是实现:通过控制球体(player)吃掉在plan面上旋转的cube(items)。
2.各物体的属性设置
这里的payer属性千万不要添加box colliider属性
右击Hierachy面板,选择UI,选择Text,调整文本框大小和位置,以及显示的内容字体和颜色如下图所示:
3.脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerCovtroller : MonoBehaviour {
public Text lbcount;
public Text lbwin;
private Rigidbody rbd;
public float Force;
private int count;
// Use this for initialization
void Start () {
rbd = GetComponent <Rigidbody> ();
lbwin.gameObject.SetActive(false);
}
// Update is called once per frame
void Update () {
}
private void FixedUpdate()//对刚体组件的操作,时间间隔要是一致的,所以不能放到
//update 方法里面去
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 moveDiy = new Vector3(h, 0, v);
rbd.AddForce(moveDiy* Force);
}
private void OnTriggerEnter(Collider other)//当进入另外一个物体时触发该方法
{
other.gameObject.SetActive(false);
RefreshUI();
}
void RefreshUI()
{
count += 1;
lbcount.text = "Eat Count:" + count;
if (count >= 8)
{
lbwin.gameObject.SetActive(true);
}
}
}
球体(player)的控制脚本。注意:对刚体的运动控制最好写在Fix update()这个方法里面,这样运行游戏时帧数才会稳定,如果写在update()里面,每一帧的更新时间取决于电脑硬件的运行速度,这就会出现卡顿的现象。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ROTATION : MonoBehaviour {
// Use this for initialization
public float RotSpeed;
void Start () {
}
// Update is called once per frame
void Update () {
transform.RotateAround(transform.position, Vector3.up, RotSpeed * Time.deltaTime);
}
}
这是小cube的旋转动画。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControll : MonoBehaviour {
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void Update () {
transform.position = player.transform.position + offset;
}
}
相机跟随代码。
初次写博客,写的很烂,虚心接受批评!