Unity_2D游戏对象的移动
2017-09-04 21:51
501 查看
结合脚本可以让游戏对象移动起来,接下来实现的是一个天鹅飞行的效果。
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; //将屏幕坐标系转化成世界坐标系 //屏幕主要转视窗位置 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.position = startPosition; }transform.Translate(Vector3.right * -speed * Time.deltaTime); } }
相关文章推荐
- Unity_2D游戏对象的移动_075
- ROLL A BALL (Unity 3D 入门) 游戏制作指导 - 移动游戏对象
- Unity相机随游戏对象的移动而移动及调整大小
- 【Unity】6.4 Transform--移动、旋转和缩放游戏对象
- Unity2D对象移动的几种方法
- Unity2d 5.1 弱联网游戏制作 (一)场景创建、添加组件、精灵移动、主摄像机跟随
- Unity 游戏对象的移动方法总结
- Unity学习笔记-2D横版游戏中的背景移动
- Unity开发2D游戏之游戏背景的移动
- [Unity]2D打飞机游戏
- Unity 朝向某个游戏对象 LookAt
- unity 使用角色控制器CharacterController移动对象造成模型动画的抖动
- Unity游戏开发的数学与物理 3 ( 让物体延任意方向移动 )
- 「Unity2D」使用Unity创建一个2D游戏系列-4
- Unity 2D游戏开发快速入门第1章创建一个简单的2D游戏
- Unity 游戏引擎的移动基本版从今天起免费提供
- Unity编写冰球对战游戏 2D版
- Unity获取游戏对象详解
- Unity 2D游戏相机跟随
- unity3d之进度条跟着游戏对象移动