Unity—实现通过按键控制2D游戏物体的移动
控制游戏物体的移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerController : MonoBehaviour
{private float speed = 5f; // 物体移动的速度private void Update(){float h = Input.GetAxis("Horizontal");transform.Translate(Vector2.right * h * speed * Time.fixedDeltaTime, Space.World);float v = Input.GetAxis("Vertical");transform.Translate(Vector2.up * v * speed * Time.fixedDeltaTime, Space.World);}
}
控制游戏物体以固定步长移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerController : MonoBehaviour
{private Vector2 CurrentPosition;private int step = 2;private int x = 0, y = 0;private void Update(){// 控制人物固定步长移动CurrentPosition = gameObject.transform.localPosition;if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)){x = step;y = 0;gameObject.transform.localPosition = new Vector2(CurrentPosition.x + x, CurrentPosition.y + y);}if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)){x = -step;y = 0;gameObject.transform.localPosition = new Vector2(CurrentPosition.x + x, CurrentPosition.y + y);}if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)){x = 0;y = step;gameObject.transform.localPosition = new Vector2(CurrentPosition.x + x, CurrentPosition.y + y);}if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)){x = 0;y = -step;gameObject.transform.localPosition = new Vector2(CurrentPosition.x + x, CurrentPosition.y + y);}}}
Unity—实现通过按键控制2D游戏物体的移动相关推荐
- Unity大魔王私藏的开发2D游戏的常用插件合集
Unity以开发3D游戏见长,早期版本的Unity在开发2D游戏时不慎方便,因此Asset Store出现了很多2D游戏开发引擎.现在Unity对2D游戏的支持越来越好,而这些开发2D游戏的Unity ...
- Unity通过键盘按键控制小球移动
首先,我们创建一个新场景. 我们需要实现的效果就是移动该小球,移动操作需要借助脚本实现. 我们创建一个Move脚本,同时将其附加给小球.同时移动操作,需要借助刚体组件实现,所以我们还需要创建一个刚体组 ...
- Unity/c#鼠标右键控制相机围绕物体旋转/滚轮控制远近
鼠标右键控制相机围绕物体旋转/滚轮控制远近 #c#代码` 鼠标右键控制相机围绕物体旋转/滚轮控制远近 using System.Collections; using System.Collection ...
- Unity 鼠标事件总结(UGUI + 游戏物体)
一.总览表+示例脚本 鼠标事件\ 物体 UGUI 3D游戏物体 移入 OnPointerEnter OnMouseEnter 移出 OnPointerExit OnMouseExit 悬置 可用OnP ...
- 【Unity3D自学记录】NGUI之显示2D游戏物体的名字
http://www.unitymanual.com/thread-14635-1-1.html 用NGUI 显示游戏物体的名字,当然也可以显示物体的血条 状态信息 最终效果 第一步 首先找到场景中的 ...
- Unity中添加组件/启用禁用游戏物体
常用变量 transform组件 场景当中的任何游戏物体,必须有一个transform组件.用来定位位置,游戏物体存在场景当中不可能没有位置,所以每个游戏物体有且只有一个transform组件,且不能 ...
- Unity+Pico 手柄按键控制
一.定义手柄按键API 1.InputDevices.GetDeviceAtXRNode,通过XRNode获取对应的设备: 2.XRNode是一个枚举类型,包含LeftEye.RightEye.Cen ...
- 【Unity3D】Unity 游戏画面帧更新 ( 游戏物体 GameObject 移动 | 借助 Time.deltaTime 进行匀速运动 )
文章目录 一. 游戏物体 GameObject 移动 二. 借助 Time.deltaTime 进行匀速运动 一. 游戏物体 GameObject 移动 在 Unity 中 , 如果想要让 游戏物体 ...
- Unity + Mirror实现原创卡牌游戏局域网联机
资源下载地址 局域网联机插件 Mirror:Mirror | 网络 | Unity Asset Store 本地客户端测试多人游戏(不用打包)插件 : ParrelSync Mirror官方文档:Ge ...
最新文章
- 异常检测——无监督、高斯分布模型,需要带标记的样本数据,基本假设:特征符合高斯分布...
- java 序列化 例子_Java序列化和反序列化例子
- 分布式系统Paxos算法
- CentOS 7 NAT软路由
- [html] 你有用过图片热区吗?它有什么运用场景?
- google+stackoverflow_解决stackOverflow打开慢的问题
- STM32CUBE——使用DWT提供毫秒延迟
- python录音功能,python实现录音功能可随时停止录音代码
- 微信小程序版本管理使用gitee
- uni-app实现仿微信前端(二)
- c语言实现10个数由小到大排序(插入排序法)
- com、cn域名到期删除时间
- 训练集、验证集、测试集的作用和区别
- QuickSearch eXtended配置
- Codeforces 115C Plumber
- Android学习之 两个Fragment之间传递信息
- 使用WebRTC搭建前端视频聊天室——信令篇
- 问道Netty。持续更新。。。
- Bye-- Czech golden generation
- 今天是端午节,工作有进展了
热门文章
- ft232h引脚_电平转换芯片FT4232H
- nyoj 541 最强DE 战斗力
- Pulsar 社区周报| 2020-12-05 ~ 2020-12-11
- android定位方式
- 网抓实例:自动修正系统日期和时间
- Android中PIN和PUK码解锁研究
- 飞鱼游戏校招美术上岸指南?看这篇就够
- 腾讯十大开源项目,最后一个太受欢迎了!
- DolphinDB 助力 WILLIAM O’NEIL 笑傲股市
- ROS_Kinetic_01 在 Ubuntu 16.04 安装ROS Kinetic 全教程附资料和镜像 2018.10.20更新