繁体   English   中英

如何让我的 2D 角色“看起来”或朝他移动的方向翻转?

[英]How to make my 2D character 'look' or flip in the direction he is moving?

我是 Unity 的新手,我正在学习/制作我的第一款游戏,这将是一款平台游戏。 我想先让运动完美。 我添加了一个简单的脚本,让玩家可以移动和跳跃。

这里是:

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

public class NewBehaviourScript : MonoBehaviour{
    public float jumpspeed = 5f;
    public float speed = 5f;
    Rigidbody2D rb;
    GameObject character;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    void Awake(){
        rb = gameObject.GetComponent<Rigidbody2D>();
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)){
            Jump();
        }
        Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
        transform.position += move *Time.deltaTime * speed;
    }
    void Jump(){
        rb.AddForce(new Vector2(0f, jumpspeed), ForceMode2D.Impulse);
    }
}

现在,我希望角色面向它移动的方向。 默认情况下,png 朝向右侧。 在此处输入图像描述

我怎样才能做到这一点? 另外,我可以让我的动作脚本更好吗?

提前致谢!

我通常使用基于 bool 的代码来执行此操作。

void FlipSprite()
{
    isFacingRight = !isFacingRight;

    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
}

暂无
暂无

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

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