首页 > 解决方案 > 当我的角色移动到不同的方向时,我的 Unity 2D 播放器控制器脚本不会让我的角色翻转

问题描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed;
    public float jumpForce;
    private float moveInput;

    private Rigidbody2D rb;

    private bool facingRight = true;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    void FixedUpdate()
    {
        moveInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

        if(facingRight == false && moveInput > 0)
        {
            Flip();
        }
        else if(facingRight == true && moveInput < 0)
        {
            Flip();
        }
    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;

    }

}

我制作了 ac# 2D 播放器控制器脚本以统一使用,但是当我点击播放时,我的播放器在移动不同方向时不会翻转并面对不同的方向。谁能发现我的脚本有什么问题?

标签: c#unity3dcontrollerflip

解决方案


没关系。只是一个愚蠢的错误。我将一个过时和更新的脚本组件放入我的播放器中。是的,我知道我很愚蠢。


推荐阅读