你遇到的问题是:
- 当对象跟随鼠标移动时,摄像机无法移动。
- 当点击放置建筑时,会创建三个建筑物而不是一个。
- 建筑物放置后,对象不会被停用。
- 资源不会被减少。
你提供的代码看起来是 Unity 脚本,似乎是使用 Unity Input System 进行输入管理。以下是我的分析和建议:
- 当对象跟随鼠标移动时,摄像机无法移动:
- 可能的原因是对象的 Collider2D 阻塞了摄像机的移动。
-
解决方法:在
OnTriggerEnter2D和OnTriggerExit2D中,移除对象的 Collider2D 或设置其为不可触发。 -
当点击放置建筑时,会创建三个建筑物而不是一个:
- 可能的原因是
Instantiate方法被调用了多次。 -
解决方法:检查
Place方法是否被调用了多次。确保只有当对象被点击放置时才调用Instantiate方法。 -
建筑物放置后,对象不会被停用:
- 可能的原因是
SetActive方法被调用了错误。 -
解决方法:检查
Place方法是否正确调用了SetActive方法。确保对象被停用后,摄像机可以移动。 -
资源不会被减少:
- 可能的原因是
Payment方法被调用了错误。 - 解决方法:检查
Payment方法是否正确减少了资源。确保资源被正确减少。
以下是修复代码:
// Placement.cs
private void Place(InputAction.CallbackContext ctx)
{
if (plassable)
{
Instantiate(batiment, position);
Payment();
// Stop using the object
gameObject.SetActive(false);
}
}
// Payment.cs
private void Payment()
{
// ...
// Reduce resources
if (costMana1 != TypeMana.None)
{
switch (costMana1)
{
case TypeMana.AirMana:
player.air_mana -= manaAmount1;
break;
// ...
}
}
// ...
}
// Placement.cs
private void OnTriggerEnter2D(Collider2D collision)
{
// Remove Collider2D to prevent camera movement blocking
if (collision.CompareTag("Batiment"))
{
bloking.Add(collision.gameObject);
// Remove Collider2D
collision.gameObject.GetComponent<Collider2D>().enabled = false;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
// Add Collider2D back
if (collision.CompareTag("Batiment"))
{
bloking.Remove(collision.gameObject);
// Add Collider2D back
collision.gameObject.GetComponent<Collider2D>().enabled = true;
}
}
// MoveCam.cs
public void EdgeMove(InputAction.CallbackContext ctx)
{
// ...
if (ctx.ReadValue<Vector2>().y > screenEdge && ctx.ReadValue<Vector2>().y < Screen.height - screenEdge &&
ctx.ReadValue<Vector2>().x > screenEdge && ctx.ReadValue<Vector2>().x < Screen.width - screenEdge)
{
_moveInput.x = 0f;
_moveInput.y = 0f;
}
}
评论 (0)