Unity2D物体仅上下移动无法向左移动问题排查
问题原因
- 代码中
Update方法里直接通过transform.position赋值位置,且用于插值的_start、_end两个参考点的X坐标均固定为0,每帧都会强制将物体的X坐标覆盖为0,即使Rigidbody2D设置了X方向的速度,也会立刻被该操作拉回固定X轴,因此无法向左移动。 - 直接修改
transform.position属于绕过Unity物理引擎的操作,会和Rigidbody2D的速度控制逻辑冲突,即便将速度赋值逻辑移入Update,速度生效的位移也会立刻被transform赋值操作覆盖,因此无法解决问题。
修复方案
提供两种常用修复方案,可根据场景选择:
方案1:基于物理引擎控制(推荐,适合需要物理碰撞的场景)
将位置修改逻辑移入物理帧回调FixedUpdate,通过Rigidbody2D提供的方法移动,保留X方向的速度位移:
public class movingRockUpScript : MonoBehaviour { public Rigidbody2D rgbody; public Vector2 velocity = new Vector2(-4, 0); Vector2 _start = new Vector2(0,-8); Vector2 _end = new Vector2(0,-3); public float _speed = 1f; private float _initOffsetX; // Start is called before the first frame update void Start() { rgbody = GetComponent<Rigidbody2D>(); // 记录初始X轴随机偏移量 _initOffsetX = Random.Range(1,5); transform.position = new Vector3(transform.position.x + _initOffsetX, transform.position.y, transform.position.z); } // 物理相关操作统一放在FixedUpdate执行 void FixedUpdate() { float t = Mathf.PingPong(Time.time, _speed) / _speed; // 仅对Y轴做上下插值,X轴保留物理速度带来的位移 float targetY = Mathf.Lerp(_start.y, _end.y, t); Vector2 targetPos = new Vector2(rgbody.position.x + velocity.x * Time.fixedDeltaTime, targetY); rgbody.MovePosition(targetPos); } }
方案2:纯Transform控制(适合无需物理碰撞的场景)
不需要Rigidbody2D的速度控制,直接在Transform赋值时累加X方向的位移:
public class movingRockUpScript : MonoBehaviour { public Vector2 velocity = new Vector2(-4, 0); Vector2 _start = new Vector2(0,-8); Vector2 _end = new Vector2(0,-3); public float _speed = 1f; private float _currentX; // Start is called before the first frame update void Start() { // 初始化X坐标(包含随机偏移) _currentX = transform.position.x + Random.Range(1,5); transform.position = new Vector3(_currentX, transform.position.y, transform.position.z); } // Update is called once per frame void Update() { // 累加X方向的位移 _currentX += velocity.x * Time.deltaTime; float t = Mathf.PingPong(Time.time, _speed) / _speed; float targetY = Mathf.Lerp(_start.y, _end.y, t); transform.position = new Vector2(_currentX, targetY); } }
内容的提问来源于stack exchange,提问作者Eric Lin
相关产品推荐
相关产品推荐

