【Space Shoot Project】moving the player
2015-10-27 21:38
633 查看
本节主要控制发射子弹的飞船
1. Assets folder中添加 Script文件夹,存放代码
2. 选中Player,Add Component - > Scripts 重命名为 PlayerController,(注意此处两个字母的大写,scripts 名字需要以大写字母开头)
3. 打开代码文件,update 是每帧渲染需要之行的代码
rigidbodies 可以用来控制物理属性,比如碰撞,重力等
由于只需要在X,Z二维控制ship,分别为 左右(Horizontal),上下(Vertical)。Y固定设置为0
input (0~1), 需要一个speed 来控制速度
控制ship 运动的范围,不要走出去了。y设置为0, X,Z 使用mathf.clamp (Ctrl+'在 文档中可查看)(取在min, max 之间的值)
定义一个Boundary类,在update函数中定义一个实例 boundary
Systerm.Serializable 可以方便在 inspector 中看到 boundary
最后的欧拉旋转,为了增强既视感,越快,Z轴 ship 会有个偏转。这样表面的反射光也会有所不同,立体感更强
完整的代码如下所示:
1. Assets folder中添加 Script文件夹,存放代码
2. 选中Player,Add Component - > Scripts 重命名为 PlayerController,(注意此处两个字母的大写,scripts 名字需要以大写字母开头)
3. 打开代码文件,update 是每帧渲染需要之行的代码
rigidbodies 可以用来控制物理属性,比如碰撞,重力等
由于只需要在X,Z二维控制ship,分别为 左右(Horizontal),上下(Vertical)。Y固定设置为0
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
input (0~1), 需要一个speed 来控制速度
控制ship 运动的范围,不要走出去了。y设置为0, X,Z 使用mathf.clamp (Ctrl+'在 文档中可查看)(取在min, max 之间的值)
定义一个Boundary类,在update函数中定义一个实例 boundary
[System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; }
Systerm.Serializable 可以方便在 inspector 中看到 boundary
最后的欧拉旋转,为了增强既视感,越快,Z轴 ship 会有个偏转。这样表面的反射光也会有所不同,立体感更强
完整的代码如下所示:
using UnityEngine;
using System.Collections;
[System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; }
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
相关文章推荐
- serialVersionUID作用
- Oracle %rowtype的用法
- Copy List with Random Pointer
- 小波函数
- Oracle常用的数据操作函数
- unity3d 播放视频
- 广西应急减灾救灾系统个人理解
- 构建可调试的openfire插件
- c++ primer第五版(中文)习题答案 第二章第三节-复合类型
- 使用python做你自己的自动化测试--对Java代码做单元测试 (2)-导入第三方jar包裹
- Java:多线程
- storm 经常使用类
- 【bzoj4276】【ONTAK2015】【Bajtman i Okrągły Robin】【二分图匹配】
- Socket编程实践(4) --多进程并发server
- 根据URL采集页面内容,封装显示在自己的页面
- uva572 Oil Deposits
- centos6.5 jdk7 ant 64位安装
- Fitnesse用系列三
- .以及JDK1.5ConcurrentHashMap新特性
- EC读书笔记系列之1:条款1、条款2、条款3