如何移动Unity中的物理Sprite?我是否像在farseers物理中一样对身体施加力量,还是移动角色精灵?这是我目前如何尝试移动它,并即时得到:在Unity中移动物理Sprite
的NullReferenceException:UnityEngine.Rigidbody2D.get_velocity()
_position.x = Input.GetAxis ("Horizontal");
_position.y = Input.GetAxis ("Vertical");
if (Input.GetKeyDown(KeyCode.Space))
{
_rig.AddForce(new Vector2(_rig.velocity.x, _jumpHeight));
_rig.velocity = new Vector2(Mathf.Clamp(_rig.velocity.x, -_clampValue.x, _clampValue.x), Mathf.Clamp(_rig.velocity.y, -_clampValue.y, _clampValue.y));
}
if (Input.GetKey(KeyCode.D))
{
//_rig.AddForce(new Vector2(_moveSpeed, _rig.velocity.x));
//_rig.velocity = new Vector2(Mathf.Clamp(_rig.velocity.x, -_clampValue.x, _clampValue.x), Mathf.Clamp(_rig.velocity.y, -_clampValue.y, _clampValue.y));
_rig.velocity = new Vector2(_position.x * _moveSpeed, _rig.velocity.y);
}
if (Input.GetKey(KeyCode.A))
{
_rig.AddForce(new Vector2(-_moveSpeed, _rig.velocity.y));
_rig.velocity = new Vector2(Mathf.Clamp(_rig.velocity.x, -_clampValue.x, _clampValue.x), Mathf.Clamp(_rig.velocity.y, -_clampValue.y, _clampValue.y));
}
从[Rigidbody2D.velocity](http://docs.unity3d.com/ScriptReference/Rigidbody2D-velocity.html ):“价值通常不是直接设定,而是通过使用力量来设定”。 – dbc
这就是我最初的做法,但是使用KeyCode.D语句只是因为原始方法不起作用。 “_rig.velocity = new Vector2(Mathf.Clamp(_rig.velocity.x,-_clampValue.x,_clampValue.x),Mathf.Clamp(_rig.velocity.y,-_clampValue.y,_clampValue.y));”用于设置速度的最小值和最大值。这就是物理原理的东西,但我会试着去看看它是如何工作的。 – KGSB
拿出所有的钳线,它并没有帮助 – KGSB