Unity 5 2D 翻转播放器

问题描述 投票:0回答:2

我制作了带有触摸位置和翻转代码的玩家脚本,以翻转玩家的运行方向,我是这样制作的,但只是从 0 翻转。如果在 x = -5 上,我触摸进入 x = 2 玩家不会翻转,但当 x = 0 时,翻转。有人可以告诉我如何做到这一点,我喜欢用地板制作,但我的玩家只在 yAxis 方向上移动一层,它会保存,但当我删除 yAxis 时,它会四处移动,我喜欢像这个游戏一样移动Game

if (gameObject.transform.position.x > 0 && faceRight)
            {
                Flip();
            }
            else if (gameObject.transform.position.x < 0 && !faceRight)
            {
                Flip();
            }
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
    private bool flag = false;
    private Vector3 endPoint;
    public float duration;
    private float yAxis;
    private bool Run = false;
    private Animator anim;
    private bool faceRight = true;

    void Start()
    {
        anim = GetComponent<Animator>();
        //save the y axis value of gameobject
        yAxis = gameObject.transform.position.y;
    }

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

        //check if the screen is touched / clicked   
        if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
        {
            //declare a variable of RaycastHit struct
            RaycastHit hit;
            //Create a Ray on the tapped / clicked position
            Ray ray;
            //for unity editor
#if UNITY_EDITOR
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //for touch device
#elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
   ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
#endif

            //Check if the ray hits any collider
            if (Physics.Raycast(ray, out hit))
            {
                //set a flag to indicate to move the gameobject
                flag = true;
                //save the click / tap position
                endPoint = hit.point;
                //as we do not want to change the y axis value based on touch position, reset it to original y axis value
                endPoint.y = yAxis;
                Debug.Log(endPoint);
            }

        }
        //check if the flag for movement is true and the current gameobject position is not same as the clicked / tapped position
        if (flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
        { //&& !(V3Equal(transform.position, endPoint))){
          //move the gameobject to the desired position
            gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1 / (duration * (Vector3.Distance(gameObject.transform.position, endPoint))));
            Run = true;
            anim.SetBool("Run", Run);

            if (gameObject.transform.position.x > 0 && !faceRight)
            {
                Flip();
            }
            else if (gameObject.transform.position.x < 0 && faceRight)
            {
                Flip();
            }
        }
        //set the movement indicator flag to false if the endPoint and current gameobject position are equal
        else if (flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
        {
            flag = false;
            Run = false;
            anim.SetBool("Run", Run);
            Debug.Log("I am here");
        }
    }

    void Flip()
    {
        faceRight = !faceRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
}
position touch 2d unity-game-engine
2个回答
0
投票

好吧,我的答案将与您的代码不同,但如果您想在向左运行时翻转字符(我假设默认为右侧),您可以简单地检查

如果角色的速度 > 0,则缩放 1 并且如果角色的速度 < 0 then scale -1

这将使角色面对你所进行的旋转,这比原始代码更容易,但如果你想实现其他目标,请告诉我,以便我可以根据你的需求纠正我的答案:)


0
投票

翻转角色似乎是

Animator
应该处理的事情。我看到你已经在使用
Animator
,所以你应该这样做:

anim.SetInteger("direction", 1)

甚至:

anim.SetFloat("position", transform.position.x)

并配置动画师进行相应操作,创建角色面向左和右的动画状态(使用游戏对象的变换

scale.x
1
-1
rotation.y
0
180
)和过渡回应您决定观看的事件。

© www.soinside.com 2019 - 2024. All rights reserved.