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

总结

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

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值