**Unity作业**
游戏名称:小球快跑
1. 场景设置
先create两个cube,我把scale设置成x为20,y和z为1,在create一个空文件,把两个cube放入,改名改成wallup和walldown。创建一个球,改成player,让scene画面处于上面为y,右面为x;然后在Game页面下面的display后面改成16:10。这样场景布置就完成了
2.简单的移动
给player设置刚体和移动,给player属性(Add Component)添加Rigbody 和script。添加完之后在Rd的位置选player再利用c#来实现player的移动,代码如下:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour {
public float speedAutoMove = 5;
public float speedMoveUpAndDown = 20;
public Rigidbody rd;
// Use this for initialization
void Start () {
rd = gameObject.GetComponent();
}
// Update is called once per frame
void Update () {
PlayerAutoMove();
PlayerMoveUpAndDown();
}
private void PlayerAutoMove()
{
rd.AddForce(Vector3.right * speedMoveUpAndDown);
}
private void PlayerMoveUpAndDown()
{
float v = Input.GetAxis("Vertical");
rd.AddForce(v * speedMoveUpAndDown * Vector3.up);
}
}
在进行wall的设置,还是利用c#来实现,代码如下:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallControl : MonoBehaviour {
private float offset;
public GameObject player;
// Use this for initialization
void Start () {
offset = gameObject.transform.position.x - player.transform.position.x;
}
void Update()
{
FollowPlayerMove();
}
// Update is called once per frame
void FollowPlayerMove () {
gameObject.transform.position = new Vector3(player.transform.position.x+offset,0,0);
}
}
再设置camera,实现相机跟随,用c#代码如下:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour {
public GameObject player;
private float offset;
// Use this for initialization
void Start () {
offset = gameObject.transform.position.x - player.transform.position.x;
}
// Update is called once per frame
void Update () {
gameObject.transform.position = new Vector3(offset+ player.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
}
}
还可以在project的空白处创建material,添加颜色用来更好的区分。
设置障碍物
把player的Cube(Mesh Filter)的Mesh改成Cube,随机建立障碍物
做完这些的截图,未完待续!



