当我翻转我的 sprite 时,它会离开 tilemaps 前景
When I flip my sprite it goes out of the tilemaps foreground
我遇到了一个我无法解决的问题。每当我移动我的角色时,如果它靠近地图的边缘,它就会通过 tilemaps collider 并离开地图。这是我截取的屏幕截图:
这是正常的:link text
这是错误发生的时间:bug
这是我用来移动和翻转角色的代码:
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
public class MovementScript : MonoBehaviour
{
Vector2 moveInput;
Animator animator;
Rigidbody2D rb;
BoxCollider2D myCollider;
PlayerStats playerStats;
float playerSpeed;
[SerializeField] float timeToWaitAfterBeingAttackedToMoveAgain = 1f;
[SerializeField] int kickbackFromEnemyAttack = 40;
void Start()
{
myCollider = GetComponent<BoxCollider2D>();
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
playerStats = GetComponent<PlayerStats>();
}
void FixedUpdate()
{
playerSpeed = playerStats.GetPlayerSpeed();
if(playerStats.PlayerIsAlive())
{
Run();
FlipSprite();
}
}
//If the player goes left the sprite flips left, otherwise it flips to the right
void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) >= Mathf.Epsilon;
if(playerHasHorizontalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f);
}
}
void Run()
{
Vector2 playerVelocity = new(moveInput.x * playerSpeed, rb.velocity.y);
rb.velocity = playerVelocity;
if (myCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
animator.SetBool("run", Mathf.Abs(rb.velocity.x) >= Mathf.Epsilon);
}
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Enemy") && playerStats.PlayerIsAlive())
{
this.enabled = false;
TriggerKickup();
StartCoroutine(ActivateMovement());
}
}
}
您最好获取 SpriteRenderer 组件并更改其 flipX 属性 而不是反转比例。
无论哪种方式,如果精灵在您翻转时以意想不到的方式移动,则可能是轴心没有按照您想要的方式设置 - 例如在这种情况下,它可能设置在精灵的左下角。
Select sprite asset,在inspector中点击“Sprite Editor”,将sprite的pivot设置为middle或bottom middle。应用更改,精灵应该从中心而不是边缘翻转。
我遇到了一个我无法解决的问题。每当我移动我的角色时,如果它靠近地图的边缘,它就会通过 tilemaps collider 并离开地图。这是我截取的屏幕截图:
这是正常的:link text
这是错误发生的时间:bug
这是我用来移动和翻转角色的代码:
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
public class MovementScript : MonoBehaviour
{
Vector2 moveInput;
Animator animator;
Rigidbody2D rb;
BoxCollider2D myCollider;
PlayerStats playerStats;
float playerSpeed;
[SerializeField] float timeToWaitAfterBeingAttackedToMoveAgain = 1f;
[SerializeField] int kickbackFromEnemyAttack = 40;
void Start()
{
myCollider = GetComponent<BoxCollider2D>();
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
playerStats = GetComponent<PlayerStats>();
}
void FixedUpdate()
{
playerSpeed = playerStats.GetPlayerSpeed();
if(playerStats.PlayerIsAlive())
{
Run();
FlipSprite();
}
}
//If the player goes left the sprite flips left, otherwise it flips to the right
void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) >= Mathf.Epsilon;
if(playerHasHorizontalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f);
}
}
void Run()
{
Vector2 playerVelocity = new(moveInput.x * playerSpeed, rb.velocity.y);
rb.velocity = playerVelocity;
if (myCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
animator.SetBool("run", Mathf.Abs(rb.velocity.x) >= Mathf.Epsilon);
}
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Enemy") && playerStats.PlayerIsAlive())
{
this.enabled = false;
TriggerKickup();
StartCoroutine(ActivateMovement());
}
}
}
您最好获取 SpriteRenderer 组件并更改其 flipX 属性 而不是反转比例。
无论哪种方式,如果精灵在您翻转时以意想不到的方式移动,则可能是轴心没有按照您想要的方式设置 - 例如在这种情况下,它可能设置在精灵的左下角。
Select sprite asset,在inspector中点击“Sprite Editor”,将sprite的pivot设置为middle或bottom middle。应用更改,精灵应该从中心而不是边缘翻转。