在某些x坐标Unity上旋转化身 - c#

我在地图上的某个位置转动虚拟形象时遇到问题。角色来回移动,但他不想在我输入的坐标处旋转。我究竟做错了什么?

这是我的代码:

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

public class AvatarPingPong : MonoBehaviour {

    // Use this for initialization
    public float speed;
    public float startXCord, endXCord;
    float endTrunx, startTurnx;

    public GameObject obj;
    Vector3 EndPoint, StartPoint;


    void Start () {
        EndPoint = transform.position;
        endXCord = EndPoint.x;
        endTrunx = EndPoint.x - 2f;

        StartPoint = transform.position;
        StartPoint.x = startXCord;
        startTurnx = StartPoint.x + 2f;
    }


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

        transform.position = new Vector3(PingPong (Time.time * speed, startXCord, endXCord ), transform.position.y, transform.position.z);

        if (transform.position.x == startTurnx ) {
            Debug.Log("start Check");
            obj.transform.Rotate(0f, 180f, 0f);
        }

        if (transform.position.x == endTrunx ) {
            obj.transform.Rotate(0f, 180f, 0f);
            Debug.Log("einde check");
        }

    }

    //function to change the default starting value of (0, 0, 0) to any value
    float PingPong(float t, float minLength, float maxLength) {
        return Mathf.PingPong(t, maxLength-minLength) + minLength;
    }   
}

参考方案

我认为问题在于,一旦他到达特定的x坐标,您就试图翻转您的化身,但他可能永远无法到达该EXACT坐标。 if (transform.position.x == startTurnx)仅在两个值完全相同并且您的虚拟形象实际上在屏幕上移动不流畅时才返回true。实际上,他每一帧都在跳分钟数,所以他可能永远也不会落在那一点上。

相反,我的建议是将他的新位置与以前的位置进行比较,以查看他的行进方向并在改变方向时翻转他。一些代码:

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

public class AvatarPingPong : MonoBehaviour {

    // Use this for initialization
    public float speed;
    public float startXCord, endXCord;
    float endTrunx, startTurnx;

    public GameObject obj;
    Vector3 EndPoint, StartPoint;

    //I'm going to assume you start it moving left. You may have to change this
    bool goingLeft = false;


    void Start () {
        EndPoint = transform.position;
        endXCord = EndPoint.x;
        endTrunx = EndPoint.x - 2f;

        StartPoint = transform.position;
        StartPoint.x = startXCord;
        startTurnx = StartPoint.x + 2f;
    }


    // Update is called once per frame
    void Update () {
        float prevX = transform.position.x;
        float newX = PingPong (Time.time * speed, startXCord, endXCord );
        transform.position = new Vector3(newX, transform.position.y, transform.position.z);


        if (newX > prevX) {
            //avatar is moving to the right, check to see if that's the direction it was going last Update
            if (goingLeft) {
                Debug.Log("Flipping Right");
                obj.transform.Rotate(0f, 180f, 0f);
                goingLeft = false;
            }
        }else if (newX < prevX){
            //avatar is moving to the left, check to see it that's the direction it was going last Update
            if (!goingLeft) {
                Debug.Log("Flipping Left");
                obj.transform.Rotate(0f, 180f, 0f);
                goingLeft = true;
            }
        }

    }

    //function to change the default starting value of (0, 0, 0) to any value
    float PingPong(float t, float minLength, float maxLength) {
        return Mathf.PingPong(t, maxLength-minLength) + minLength;
    }   
}

TypeError:_transform()接受2个位置参数,但给出了3个 - python

我了解了CaesarCipher:In [90]: !cat caesar_cipher.py class CaesarCipher: """Construct Caesar cipher using given integer shift for rotation.""" def __init__…

LeetCode题解计算机为什么是基于二进制的?

可以是三进制么?二进制有什么好处?题解:为什么叫电子计算机?算盘应该没有二进制

LeetCode题解统计城市的所有灯泡

这个是我刚毕业的时候,一个真实的面试题,这是一个开放题。题目描述:想办法,将一个城市的所有灯泡数量统计出来。题解:费米估算法1、如果某个城市常驻人口有1000万2、假设每5人居住在一套房里,每套房有灯泡5只,那么住宅灯泡共有1000万只3、假设公众场所每10人共享一只灯泡,那么共有100万只4、主要的这两者相加就得出了1100万只当然实际上这是估算的,具体应…

LeetCode题解黑白圆盘

一个圆盘被涂上了黑白二色,两种颜色各占一个半圆。圆盘以一个未知的速度、按一个未知的方向旋转。你有一种特殊的相机可以让你即时观察到圆上的一个点的颜色。你需要多少个相机才能确定圆盘旋转的方向?题解:可以用一个相机即可

LeetCode题解圆上任取三点构成锐角三角形的概率

来自字节跳动的一道几何题题解:1/4