栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity 3D 入门小游戏 小球酷跑(上)

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity 3D 入门小游戏 小球酷跑(上)


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 ();
	}
	
	// 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);
	}
}

总结

以上就是今天要讲的内容,本文简单介绍了小球酷跑游戏的环境搭建和一部分功能的设置,剩下的障碍物以及得分等功能设置下次再讲啦,拜拜啦!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901151.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号