简体   繁体   English

我有一个嵌套在播放器下方的空白 Unity object。 它应该绑在精灵的前面,但不会随精灵翻转

[英]I have a blank Unity object nested under the player. It's supposed to be tied to the front of the sprite but does not flip with the sprite

I've got my player sprite that has a blank "Front Check" object nested underneath it.我的播放器精灵有一个空白的“前检查”object 嵌套在它下面。 The nested item is used to check if the front of the sprite is touching a wall.嵌套项用于检查精灵的前面是否接触到墙壁。 the problem I'm having is the Front Check object does not flip with the sprite when I move in the opposite direction.我遇到的问题是当我向相反方向移动时,前检查 object 不会与精灵一起翻转。 it was working fine earlier today and then out of the blue just quit.它今天早些时候工作正常,然后突然退出。 This is my first unity project and I'm just not quite sure what's missing.这是我的第一个统一项目,我只是不太确定缺少什么。 Any help is much appreciated.任何帮助深表感谢。 here is an image of the project and my movement script.这是该项目的图像和我的运动脚本。 在此处输入图像描述

    using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody2D rb;
    private BoxCollider2D coll;
    private SpriteRenderer sprite;
    private Animator anim;

    [SerializeField] private LayerMask jumpableGround;

    public float checkRadius;
    bool isTouchingFront;
    public Transform frontCheck;
    public float wallSlidingSpeed;
    bool wallSliding;
    bool wallJumping;
    public float xWallForce;
    public float yWallForce;
    public float wallJumpTime;

    private float dirX = 0f;
    [SerializeField] private float moveSpeed = 7f;
    [SerializeField] private float jumpForce = 14f;
    [SerializeField] private float airControl;

    private enum MovementState { idle, running, jumping, falling }

    [SerializeField] private AudioSource JumpSoundEffect;

    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();
        sprite = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    private void Update()
    {
        dirX = Input.GetAxisRaw("Horizontal");

        if (dirX < 0 && IsGrounded())
        {
            rb.velocity = new Vector2(moveSpeed * -1, rb.velocity.y);
        }
        else if (dirX > 0 && IsGrounded())
        {
            rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
        }
        else if (dirX == 0 && IsGrounded())
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }
  


        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            JumpSoundEffect.Play();
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);

        }



        UpdateAnimationState();
    }

    private void UpdateAnimationState()
    {
        MovementState state;

        if (dirX > 0f && IsGrounded())
        {
            state = MovementState.running;
        }
        else if (dirX < 0f && IsGrounded())
        {
            state = MovementState.running;
        }
        else
        {
            state = MovementState.idle;
        }

        if (rb.velocity.y > .1f)
        {
            state = MovementState.jumping;
        }
        else if (rb.velocity.y < -.1f)
        {
            state = MovementState.falling;
        }

        //Wall Jump
        isTouchingFront = Physics2D.OverlapCircle(frontCheck.position, checkRadius, jumpableGround);

        if (isTouchingFront == true && IsGrounded() == false && dirX != 0)
        {
            wallSliding = true;
        }
        else
        {
            wallSliding = false;
        }

        if (wallSliding)
        {
            rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
        }

        if (Input.GetButtonDown("Jump") && wallSliding == true)
        {
            wallJumping = true;
            Invoke("SetWallJumpingToFalse", wallJumpTime);
        }

        if (wallJumping == true)
        {
            rb.velocity = new Vector2(xWallForce * -Input.GetAxisRaw("Horizontal"), yWallForce);
        }



        anim.SetInteger("state", (int)state);

    }

    void SetWallJumpingToFalse()
    {
        wallJumping = false;
    }

    public bool IsGrounded()
    {
        return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);

    }
    public void FixedUpdate()
    {
        if (dirX > 0f)
        {
            sprite.flipX = false;
        }
        else if (dirX < 0f)
        {
            sprite.flipX = true;
        }
    }


}

sprite.flipX only flips the rendering not the actual object scaling. sprite.flipX只翻转渲染而不是实际的 object 缩放。

You rather want to use eg您宁愿使用例如

var scale = transform.localScale;
scale.x = Mathf.Sign(dirX);
transform.localScale = scale;

Note that it needs to be UnityEngine.Mathf.Sign and not System.Math.Sign since the latter returns 0 for parameter 0 which would totally break your colliders etc. while Mathf.Sign returns only -1 or 1 .请注意,它需要是UnityEngine.Mathf.Sign不是System.Math.Sign ,因为后者为参数0返回0 ,这将完全破坏您的对撞机等,而Mathf.Sign仅返回-11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM