You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity新手求助:使用transform.forward旋转角色时出现意外移动问题

问题解决方法

你的角色同时出现旋转和移动的情况,是因为代码中保留了直接修改位置的语句。只需移除或注释掉移动位置的那一行即可:

Vector2 inputVector = new Vector2(0,0);

inputVector = inputVector.normalized;

Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y);

// 移除下面这行(或注释),你已有独立的移动机制
// transform.position += moveDir * normalSpeed * Time.deltaTime;

float rotateSpeed = 10f;
transform.forward = Vector3.Slerp(transform.forward, moveDir, Time.deltaTime * rotateSpeed);

原理说明

  • transform.position += ... 这行代码直接修改角色的位置坐标,是导致移动的核心原因
  • transform.forward = Vector3.Slerp(...) 仅负责让角色朝向目标方向平滑旋转,不会触发移动行为

内容的提问来源于stack exchange,提问作者Cristoferos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 18:32:03