Unity_2D游戏对象的移动_075
2017-09-02 11:46
363 查看
通过前面对创建动画帧的学习,我们结合脚本让游戏对象移动起来此时用的是一个天鹅飞行的效果。
在精灵上绑定此脚本:
效果如下:
在精灵上绑定此脚本:
using UnityEngine; using System.Collections; public class SwanMove : MonoBehaviour { public float speed; private float width; private float height; private Vector3 startposition; // Use this for initialization void Start () { //计算精灵自身的尺寸大小 width = transform.GetComponent<Renderer>().bounds.extents.x; height = transform.GetComponent<Renderer>().bounds.extents.y; transform.position = new Vector3(0 + width,0,0); //将屏幕坐标系转化成世界坐标系 Vector3 moveWidth = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0)); startposition = new Vector3(moveWidth.x + width, 0, 0); //把天鹅放在起始位置 transform.position = startposition; } // Update is called once per frame void Update () { //如果精灵在屏幕内移动 if (transform.position.x>-startposition.x) { transform.Translate(Vector3.right * -speed * Time.deltaTime); } else { //如果精灵超出屏幕的边界 那么就重新置为起始位置 transform.position = startposition; } } }
效果如下:
相关文章推荐
- Unity_2D游戏对象的移动
- 【Unity】6.4 Transform--移动、旋转和缩放游戏对象
- ROLL A BALL (Unity 3D 入门) 游戏制作指导 - 移动游戏对象
- Unity开发2D游戏之游戏背景的移动
- Unity2D对象移动的几种方法
- Unity2d 5.1 弱联网游戏制作 (一)场景创建、添加组件、精灵移动、主摄像机跟随
- Unity 游戏对象的移动方法总结
- Unity学习笔记-2D横版游戏中的背景移动
- Unity相机随游戏对象的移动而移动及调整大小
- 「Unity2D」使用Unity创建一个2D游戏系列-6
- 游戏的物理和数学:Unity中的弹道和移动目标提前量计算
- Unity编写冰球对战游戏 2D版
- Unity之实时渲染场景中的游戏对象
- 游戏的物理和数学:Unity中的弹道和移动目标提前量计算
- 即时战略游戏中如何协调对象移动
- Unity4.x 2D游戏开发基础教程第1章Unity及其组成的介绍
- 网页及移动平台2D游戏开发探索
- Unity克隆游戏对象
- Unity学习笔记2 简易2D横版RPG游戏制作(二)
- Unity之路(七):游戏对象跟随鼠标移动示例