看了几个教程,这个是怕学了又忘而写的,可能比较简略,最后面会放一下我看过的几个教程,觉得写得比较详细,跟我一样的新手也许可以去看一下嗷(*′口`)
一.建立平面及小球(通过左上的create-3D Object选择立方或球体),添加灯光(create-Light);
平面及小球的颜色可以通过新建一个文件夹储存各种颜色(可命名为Material),右键-create-material来创建,完成后把想要的颜色的material拖到目标物体就行辣;
二.给小球(player)添加刚体(Rigidbody),使之有物理效果;给小球添加脚本(Script),使小球根据键盘输入的上下左右运动;
1.添加刚体:Add Component - Rigidbody
2.脚本:Add Component - New Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playercontroller : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float horizontal_move = Input.GetAxis("Horizontal");//控制左右
float vertical_move = Input.GetAxis("Vertical");//控制前后
GetComponent<Rigidbody>().AddForce(new Vector3(horizontal_move, 0, vertical_move)*10);//添加一个让物体移动的力,因为不上下移动,所以y轴为0
}
}
控制小球的左右前后移动的输入键默认为上下左右即WSAD键,但是也可以通过edit-project settings来另行设置
三. 相机跟随小球移动
对Main Camera建立脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class followplayer : MonoBehaviour {
// Use this for initialization
void Start () {
}
public GameObject Player;//会在Main Camera的Inspector里面出现这一项,在此选中小球(我命名为player)即可
// Update is called once per frame
void Update () {
Vector3 position_move = Player.GetComponent<Transform>().position;
this.GetComponent<Transform>().position = new Vector3(position_move.x, position_move.y + 5.28f, position_move.z - 3);//在小球的位置基础上保持一定距离,这个多出的距离是按照把Main Camera拖到player上,调整到适当的位置后显示的;记下这几个数字后再把main camera 拖回去_(:зゝ∠)_
}
}
四.建立食物,调整角度,建立脚本使之可以自己旋转
先只建立一个就行了!之后可以通过右键create-prefab来大量生产属性相同的食物
具体操作就是!新建prefab后给它命名为food,然后将之前建立的食物拖到上面,变成这样:
建立脚本来使food有自己旋转的功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class food : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Rotate(Vector3.up);//旋转
}
}
然后拖住这个food放到平面上去来建立多个相同属性的食物
接着下一篇Roll A Ball 小游戏,Unity入门简单记录(二)
然后贴一下我看的那几个教程哈╰(´︶`)╯♡:
Unity官方实例教程 Roll-a-Ball
教你做Unity第一个游戏Roll A Ball