Roll A Ball 小游戏,Unity入门简单记录(一)

看了几个教程,这个是怕学了又忘而写的,可能比较简略,最后面会放一下我看过的几个教程,觉得写得比较详细,跟我一样的新手也许可以去看一下嗷(*′口`)

一.建立平面及小球(通过左上的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值