1.游戏环境搭建
2.玩家运动设置
3.相机跟随设置
4 墙体控制设置
一、游戏环境搭建
1.墙体搭建
在场景中新建一个cube将它拉长到一个适当的长度设为上墙,位置和长度参考如下:
克隆下墙,位置和长度参考如下:
2.玩家设置
新建一个球体作为玩家,将其放到一个适当的位置。
3.场景效果
二、玩家运动设置
在玩家上添加刚体属性,使其能够拥有物理属性。新建一个名为playermover的c#文件,将其挂到玩家上,使能够通过电脑的上下键控制玩家的上下跳跃。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMove : MonoBehaviour {
public Rigidbody rd;
public float speedAutoMove = 10;
//public float offset_camera;
// Use this for initialization
void Start () {
rd = gameObject.GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
PlayerAutoMove ();
PlayerMoveUpAndDown ();
}
private void PlayerAutoMove()
{
rd.AddForce (Vector3.right * speedAutoMove);
}
private void PlayerMoveUpAndDown()
{
float v = Input.GetAxis ("Vertical");
rd.AddForce (v * Vector3.up * 20);
}
}
三、相机跟随设置
后期小球会长距离移动,为了能够使小球一直出现在游戏界面中,就要往小球上加一个相机跟随。新建一个名为CameraControl 的c#文件,将其挂到相机上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour {
public GameObject player;
private float offset_camera;
// Use this for initialization
void Start () {
offset_camera = gameObject.transform.position.x - player.transform.position.x;
}
// Update is called once per frame
void Update () {
FollowCameraMove ();
}
void FollowCameraMove()
{
gameObject.transform.position = new Vector3 (offset_camera + player.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
}
}
四、墙体控制设置
新建一个名为wallControl 的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;
}
// Update is called once per frame
void Update () {
FollowPlayerMove ();
}
void FollowPlayerMove()
{
gameObject.transform.position = new Vector3 (player.transform.position.x+offset,0,0);
}
}
总结
以上就是今天要讲的内容,本文简单介绍了小球酷跑游戏的环境搭建和一部分功能的设置,剩下的障碍物以及得分等功能设置下次再讲啦,拜拜啦!