惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
博客园 - Franky
V
V2EX
爱范儿
爱范儿
J
Java Code Geeks
小众软件
小众软件
Last Week in AI
Last Week in AI
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
酷 壳 – CoolShell
酷 壳 – CoolShell
The Register - Security
The Register - Security
GbyAI
GbyAI
Vercel News
Vercel News
Y
Y Combinator Blog
腾讯CDC
F
Fortinet All Blogs
I
InfoQ
N
Netflix TechBlog - Medium
B
Blog RSS Feed
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
Scott Helme
Scott Helme
T
Tor Project blog
U
Unit 42
Google Online Security Blog
Google Online Security Blog
PCI Perspectives
PCI Perspectives
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
aimingoo的专栏
aimingoo的专栏
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - sun_dust_shadow

(翻译) 延迟补偿方法的协议设计与优化 Latency Compensating Methods in Client/Server In-game Protocol Design and Optimization (翻译) Dota2 Random Distribution Dota2中的随机分布 (个人思考)游戏技能的实现 (读书笔记)平衡掌控者 [瞄准辅助] 实现一种柔和平滑的瞄准辅助 (个人思考)实现游戏GAS系统中的Tag (翻译) Efficiency Tips on Switching Spaces and Transformation Matrices in Unity (翻译) V Rising's Animation Layering in Unity (翻译 unity2020.1) Understanding the managed heap (UnityEditor Tool) 语音控制unity editor暂停和播放 (翻译 ue gas) Gameplay Ability 总结 Overwatch Gameplay Architecture and Netcode 守望先锋的游戏架构与网络代码 (翻译 ) Source Multiplayer Networking Source引擎的多人网路系统 (翻译 gafferongames) Networked Physics in Virtual Reality VR中的网络物理 (翻译 gafferongames) Client Server Connection 客户端服务器连接 (翻译 gafferongames)Reliable Ordered Messages 可靠有序消息 (翻译 gafferongames) Sending Large Blocks of Data 发送大块数据 (翻译 gafferongames) Packet Fragmentation and Reassembly 数据包分片与重组 游戏中Shotgun(喷子)发射子弹的实现
Topdown游戏中Input朝向的转化
sun_dust_shadow · 2025-04-07 · via 博客园 - sun_dust_shadow

前提

在Topdown双摇杆游戏中,我们需要将摇杆输入朝向转到摄像机空间,以实现往前走是往屏幕的前方,往后是往屏幕的后方,而不是原始的输入朝向。

我们只讨论固定视角并且player只有y轴旋转的游戏。

实现

需要进行一个Vector2的映射

 1 private static Vector3 ViewSpaceToGroundSpace(float cameraAngleY, Vector2 vec)
 2 {
 3     float angleRad = -cameraAngleY * Mathf.Deg2Rad;
 4     float cos = Mathf.Cos(angleRad);
 5     float sin = Mathf.Sin(angleRad);
 6 
 7     Vector2 rotated = new Vector2(
 8         vec.x * cos - vec.y * sin,
 9         vec.x * sin + vec.y * cos
10     );
11 
12     return new Vector3(rotated.x, 0f, rotated.y);
13 }

旋转和瞄准

双摇杆下我们需要区分旋转和瞄准的实现。

其中旋转的实现,鼠标和摇杆是一致的

1       private static Vector3 GetMovementInputGroundSpace(Transform camera, int horizontalActionId, int verticalActionId, float deadZone)
2         {
3             var input = InputExtension.GetAxis2D(horizontalActionId, verticalActionId);
4             if (input.sqrMagnitude > 1) input = input.normalized;
5             input = InputExtension.InputWithRadialDeadZone(input.x, input.y, deadZone);
6             return ViewUtils.ViewSpaceToGroundSpace(camera, input).normalized;
7         }

瞄准朝向的获取,鼠标相对复杂一点,需要在开火的水平面做映射

 1         private static Vector3 GetAimInputInGroundSpace(GameEntity gameEntity, Camera camera, int horizontalActionId, int verticalActionId)
 2         {
 3             if (InputExtension.IsKeyboarding())
 4             {
 5                 if (InputExtension.GetButton(InputAction.Aim))
 6                 { 9                     var playerFirePoint = gameEntity.GetObject<WeaponFirePointTag>().Component;
10                     Transform playerTransform = gameEntity.Get<Transform>();
11                     Vector3 refPosition = playerTransform.position;
13                     refPosition.y = playerFirePoint.position.y;
14                     var ray = camera.ScreenPointToRay(InputExtension.MousePosition);
15                     //we need a plane to project the aim direction to the aiming ground : https://forum.unity.com/threads/3rd-person-look-at-mouse-script.1078358/
16                     var aimingPlane = new Plane(Vector3.up, refPosition);
17                     aimingPlane.Raycast(ray, out float distance);
18                     var aimDestDirection = (ray.origin + distance * ray.direction - refPosition).normalized;
19                     var rotation = Quaternion.LookRotation(aimDestDirection);
20                     return rotation * Vector3.forward;
21                 }
22 
23                 return Vector3.zero;
24             }
25 
26             var input = InputExtension.GetAxis2D(horizontalActionId, verticalActionId);
27             return ViewUtils.ViewSpaceToGroundSpace(camera, input).normalized;
28         }