C# 碰撞在二维中不起作用
我在Unity中制作了2D墙,但我的角色可以穿过它。发生了什么?我的角色有Rigibody2D和BoxCollider 2D,墙上有BoxCollider。 字符移动代码:C# 碰撞在二维中不起作用,c#,unity3d,C#,Unity3d,我在Unity中制作了2D墙,但我的角色可以穿过它。发生了什么?我的角色有Rigibody2D和BoxCollider 2D,墙上有BoxCollider。 字符移动代码: Vector2 moveVec = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"),CrossPlatformInputManager.GetAxis("Vertical")); moveVec = moveVec * moveForce;
Vector2 moveVec = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"),CrossPlatformInputManager.GetAxis("Vertical"));
moveVec = moveVec * moveForce;
transform.Translate (moveVec);
我的角色有Rigibody2D和BoxCollider
如果使用Rigibody2D
,则还必须使用BoxCollider2D
而不是BoxCollider
。确保墙上也有它的boxcollizer2d
使用transform.Translate
或transform.position
移动对象时无碰撞。如果您的游戏对象具有RigidBy2D
附加到它之后,必须使用rigiidbody2d.velocity
,rigiidbody2d.AddForce
(rigiidbody2d.AddXXX
)或rigiidbody2d.MovePosition
)移动它
最好在FixedUpdate()函数中执行此特定操作。另外,我认为应该使用GetAxisRaw
而不是GetAxis
,这样播放器会在松开键/手指后立即停止
public float speed = 2f;
Rigidbody2D rg2d;
void Start()
{
rg2d = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
float h = CrossPlatformInputManager.GetAxisRaw("Horizontal");
float v = CrossPlatformInputManager.GetAxisRaw("Vertical");
Vector2 tempVect = new Vector2(h, v);
tempVect = tempVect.normalized * speed * Time.fixedDeltaTime;
rg2d.MovePosition((Vector2)transform.position + tempVect);
}
public float speed=2f;
刚体2d-rg2d;
void Start()
{
rg2d=GetComponent();
}
void FixedUpdate()
{
float h=CrossPlatformInputManager.GetAxisRaw(“水平”);
float v=CrossPlatformInputManager.GetAxisRaw(“垂直”);
向量2 tempVect=新向量2(h,v);
tempVect=tempVect.normalized*速度*时间.固定时间;
rg2d.MovePosition((Vector2)transform.position+tempVect);
}
如果移动太快/太慢,您可以降低/提高速度。您的角色是否有BoxCollizedR2D(请注意末尾的2D)。墙也一样。是的,它有BoxCollider2Dtry来使用rigidbody.MovePosition而不是transform.Translate。同时检查碰撞遮罩和对象层如果在墙上添加Rigidbody2D怎么办?仍然没有发生任何事情您应该使用Time.fixedDeltaTime@JuanBayonaBeriso没错。谢谢你的提醒,谢谢。update和fixedupdate有什么区别?+我不知道怎么回事,但不管速度值是多少,对象都以相同的速度移动