作者:软件猫

日期:2016年12月8日

转载请注明出处:http://www.cnblogs.com/softcat/p/6144041.html

上一篇文章中存在一个很严重的问题,首先我们先让 Main Camera 跟随 Player 移动

Main Camera 跟随 与 阻挡物

用 Redux 模式,我们可以很轻易的实现这个功能。

1、创建 MainCameraViewProvider.cs

using UnityEngine;

public class MainCameraViewProvider : ViewProvider
{
protected override void OnFixedStateChanged (State state)
{
// 使用 Player 的 x,y 坐标,并保持 z 坐标
transform.position = new Vector3 (state.Player.Position.x, state.Player.Position.y, transform.position.z);
}
}

2、将 MainCameraViewProvider.cs 拖放至 Main Camera。

3、使用下面的图片创建一个新的精灵,并拖入场景,命名为 Rock。

4、给 Rock 添加 Box Collider 2D

执行游戏后,我们发现当 Player 被 Rock 挡住的时候,MainCamera 仍然在向前移动。这是因为 Rigidbody 发现 Player 被 Rock 阻挡了,阻止了 Player 的移动,但是 AxisReducer 仍然在改变 PlayerState 中的 Position。

下面要针对这个问题进行改造。

改造 Rigidbody2D

改造思路是这样的:InitAction 时,传入 Player 的 Rigidbody2D 组件,用 Rigidbody2D 来决定 Position 和 Rotation。

下面的代码中,加粗部分是新增以及修改后的代码,划线部分是删除的代码。

1、改造 PlayerState.cs

using UnityEngine;

namespace Player
{
public class PlayerState
{
// 用于控制位置的刚体
public Rigidbody2D Rigidbody { get; set; }
// 玩家坐标
public Vector2 Position {
get { return Rigidbody.position; }
set
{ Rigidbody.MovePosition (value); }
} // 玩家面向的方向
public float Rotation {
get { return Rigidbody.rotation; }
set
{ Rigidbody.MoveRotation (value); }
} // 移动速度
public float Speed { get; set; } }
}

2、改造 PlayerActions.cs

using UnityEngine;

namespace Player
{
// Player 初始化,设置坐标、旋转角度与移动速度
public class InitAction : IAction
{
public Vector2 position { get; set; }
public float rotation { get; set; }
public float speed { get; set; } public Rigidbody2D rigidbody { get; set; }
} // 移动轴
public class AxisAction : IAction
{
public float x { get; set; } public float y { get; set; }
}
}

3、改造 PlayerViewProvider.cs

using UnityEngine;

namespace Player
{
public class PlayerViewProvider: ViewProvider
{
[SerializeField]
float speed = 3f; Rigidbody2D rigid = null; void Start ()
{
rigid = GetComponent<Rigidbody2D> (); // 执行初始化
Store.Dispatch (new InitAction () {
position = transform.position,
rotation = transform.rotation.eulerAngles.z,
speed = this.speed,
rigidbody = GetComponent<Rigidbody2D> (),
});
} void FixedUpdate ()
{
// 获取轴数据,并传递 Action
float ax = Input.GetAxis ("Horizontal");
float ay = Input.GetAxis ("Vertical"); if (ax != || ay != ) {
Store.Dispatch (new AxisAction () { x = ax, y = ay });
}
} protected override void OnFixedStateChanged (State state)
{
if (rigid != null) {
// 刚体旋转和移动
rigid.MoveRotation (state.Player.Rotation);
rigid.MovePosition (state.Player.Position);
}
} }
}

执行游戏,刚体问题解决了。

这里有一点违反 Redux 模式,但是并不会影响整个架构。就这样。

Unity 之 Redux 模式(第二篇)—— Rigidbody 改造,摄像机控制的更多相关文章

  1. Unity 之 Redux 模式(第一篇)—— 人物移动

    作者:软件猫 日期:2016年12月6日 转载请注明出处:http://www.cnblogs.com/softcat/p/6135195.html 在朋友的怂恿下,终于开始学 Unity 了,于是有 ...

  2. 第二篇 :微信公众平台开发实战Java版之开启开发者模式,接入微信公众平台开发

    第一部分:微信公众号对接的基本介绍 一.填写服务器配置信息的介绍 登录微信公众平台官网后,进入到公众平台后台管理页面. 选择 公众号基本设置->基本配置 ,点击“修改配置”按钮,填写服务器地址( ...

  3. Spring Cloud第二篇 | 使用并认识Eureka注册中心

    ​ 本文是Spring Cloud专栏的第二篇文章,了解前一篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 ​​ 一.Sprin ...

  4. 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)

    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  5. 深入理解javascript对象系列第二篇——属性操作

    × 目录 [1]查询 [2]设置 [3]删除[4]继承 前面的话 对于对象来说,属性操作是绕不开的话题.类似于“增删改查”的基本操作,属性操作分为属性查询.属性设置.属性删除,还包括属性继承.本文是对 ...

  6. [转]Android开源项目第二篇——工具库篇

    本文为那些不错的Android开源项目第二篇--开发工具库篇,主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多 ...

  7. 【OpenGL】第二篇 Hello OpenGL

    ---------------------------------------------------------------------------------------------------- ...

  8. 深入理解javascript函数系列第二篇——函数参数

    × 目录 [1]arguments [2]内部属性 [3]函数重载[4]参数传递 前面的话 javascript函数的参数与大多数其他语言的函数的参数有所不同.函数不介意传递进来多少个参数,也不在乎传 ...

  9. IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm(转载)

    IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm 自从本系列发布之后,收到了很多的朋友的回复!非常感谢,同时很多朋友问到了一些问题,有些问 ...

随机推荐

  1. UVA_埃及分数(Hard Version) UVA 12588

    Problem EEg[y]ptian Fractions (HARD version)Given a fraction a/b, write it as a sum of different Egy ...

  2. 剑指offer-面试题3.二维数组中的查找

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增 的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断该数组中是否有该整数. 算法流程如下: 比如一个 ...

  3. Unity 2DSprite

    Unity官方意识到在4.3版本之前,并没有自带的支持2D游戏工具,商店里面有很多有名2D插件Uni2D,2DToolkit,在4.3版本之后就出现UISprite精灵来支持2D游戏开发,我用这个很多 ...

  4. IOS 排序算法

    /** * @brief 冒泡排序法 * * @param arr 需要排序的数组 */ -(void)BubbleSort:(NSMutableArray *)arr { // 取第一个与其邻接的对 ...

  5. css中bug记录

    1.margin塌陷,通俗叫法. 假如一个盒子box里边嵌套了两个盒子(记为box1,box2).box1的margin-top不会如预想的一样在box顶部撑开一个边距,而是以包含box的容器上边界为 ...

  6. oracle取分组的前N条数据

    select * from(select animal,age,id, row_number()over(partition by animal order by age desc) row_num ...

  7. NodeJS初学者实战之旅(I) —— 介绍、目录

    旅行之初 作为一个工作了两年多后端开发人员,未接触过NodeJS,对它的认知也仅仅停留在“可以使用JavaScript代码编写服务端程序”上.最近空闲时间 较多,便想来啃它一啃.但是没有一个实际的玩意 ...

  8. myeclipse内存不足有关问题

    myeclipse内存不足有关问题 myeclipse内存不足问题 使用myeclipse8.5出现如下问题:MyEclipse has detected that less than 5% of t ...

  9. es3中使用es6/7的字符串扩展

    最近在看阮一峰的<ES6标准入门>,在字符串扩展一节中有提到几个新的扩展,觉得挺有意思,想在ES3里面使用,于是就有下面的兼容性写法. repeat 将一个字符串重复n次 String.p ...

  10. HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)

    先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 #include <iostream> #inc ...