首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我有一个空的嵌套在播放器下面。它应该被绑在雪碧的前面,但不能和雪碧一起翻

我有一个空的嵌套在播放器下面。它应该被绑在雪碧的前面,但不能和雪碧一起翻
EN

Stack Overflow用户
提问于 2022-01-28 04:45:41
回答 1查看 35关注 0票数 0

我有我的玩家精灵有一个空白的“前端检查”对象嵌套在它下面。嵌套项用于检查sprite的前面是否接触到了墙壁。我遇到的问题是当我向相反的方向移动时,Front对象不会与sprite一起翻转。今天早些时候天气很好,突然间就辞职了。这是我的第一个统一项目,我只是不太确定缺少了什么。任何帮助都是非常感谢的。这是一个项目的图片和我的运动脚本。

代码语言:javascript
运行
复制
    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;
        }
    }


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-28 23:07:04

sprite.flipX只翻转呈现,而不是实际的对象缩放。

你很想用。

代码语言:javascript
运行
复制
var scale = transform.localScale;
scale.x = Mathf.Sign(dirX);
transform.localScale = scale;

注意,它需要是UnityEngine.Mathf.Sign,而不是 System.Math.Sign,因为后者返回参数00,这将完全破坏对撞机等等,而Mathf.Sign只返回-11

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70888917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档