0
点赞
收藏
分享

微信扫一扫

unity简易跑酷避障小游戏(零基础可做)


Unity 小球酷跑开发过程

  • ​​场景的搭建​​
  • ​​创建主角player​​
  • ​​设置摄像机画面​​
  • ​​游戏逻辑代码的编辑​​
  • ​​主角player的逻辑代码​​
  • ​​长方体墙体的跟随移动代码​​
  • ​​相机跟随代码​​

场景的搭建

创建两个长方体围栏:在物体栏右键点击 选择3D Object ==> cube ,点击之后发现已经创建了一个正方体。重复刚才的操作再创建一个正方体。

unity简易跑酷避障小游戏(零基础可做)_移动代码


两个正方体创建完成之后操控其形变成为长方体。

unity简易跑酷避障小游戏(零基础可做)_c#_02


点击正方体,将其Scale属性的X值改为20,命名改为wallDown和wallUp,便于管理。并且将两个长方体调到如图合适位置。

unity简易跑酷避障小游戏(零基础可做)_unity_03


右键点击,选择Create Empty ,创建一个空物体,将wallDown和wallUp拖进空物体,并将空物体命名wall,便于管理资源。

unity简易跑酷避障小游戏(零基础可做)_c#_04

创建主角player

前面的方法创建一个正方体,命名为player,设置刚体属性rigidbody,如果找不到,就在下方的add component搜索rigidbody即可。

unity简易跑酷避障小游戏(零基础可做)_移动代码_05

设置摄像机画面

选中摄像机,再右侧属性栏中修改图中的三个值。

1.clear flags 值改为solid color

2.background的值改成自己喜欢的颜色

3.projection值改为orthgraphic

unity简易跑酷避障小游戏(零基础可做)_c#_06

游戏逻辑代码的编辑

首先创建命名为 script 的文件夹,便于代码的统一管理。右键点击,选择create ==> folder 创建文件夹。

unity简易跑酷避障小游戏(零基础可做)_创建文件夹_07

主角player的逻辑代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playMove : MonoBehaviour
{
public Rigidbody rd;
public float speedAutoMove=5;
public float speedMoveUpandDown=20;
// Start is called before the first frame update
void Start()
{
rd=gameObject.GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
PlayAutoMove();
PlayMoveUpandDown();
}
private void PlayAutoMove(){
rd.AddForce(Vector3.right*speedAutoMove);//前进
}
private void PlayMoveUpandDown(){
float v=Input.GetAxis("Vertical");//上下运动
rd.AddForce(v*Vector3.up*speedMoveUpandDown);//给一个向上的力量
}
}

新建代码文件:右键点击,选择 create ==> c# script , 然后命名为playMove (其他也可,此次只做演示)

unity简易跑酷避障小游戏(零基础可做)_右键_08

代码编辑完成之后挂载到主角player身上,(如何挂载:拖动代码到物体上 或者 直接在物体的属性栏的add 点击之后添加 New script即可)

unity简易跑酷避障小游戏(零基础可做)_c#_09


unity简易跑酷避障小游戏(零基础可做)_unity_10

长方体墙体的跟随移动代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class wallControl : MonoBehaviour
{
private float offset;
public GameObject player;
// Start is called before the first frame update
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);
}
}

(如何创建文件夹,此次不再演示)代码编辑完成之后命名wallControl,挂载到管理长方体墙体的空物体上,挂载完之后,先选中wall墙体,然后拖动 player 物体到属性栏这里(如图),使none变为player

unity简易跑酷避障小游戏(零基础可做)_创建文件夹_11

相机跟随代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cameraControl : MonoBehaviour
{
public GameObject player;
private float offset_camera;
// Start is called before the first frame update
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);
}
}

代码编辑完成之后,命名cameraControl,挂载到摄像机上,挂载完成之后,同样操作,先选中摄像机,然后拖动player 到右侧的属性栏这里,使none变为player。

unity简易跑酷避障小游戏(零基础可做)_右键_12


至此,这个简易的小游戏基本逻辑就完成了,可以在player的必经道路上设置障碍,控制小球跳过(向上方向键控制小球上升跳动)障碍了。


举报

相关推荐

0 条评论