gpt4 book ai didi

c# - 在某个x坐标Unity旋转头像

转载 作者:行者123 更新时间:2023-12-03 22:58:37 24 4
gpt4 key购买 nike

我在将头像转向 map 上的某个位置时遇到问题。角色来回移动,但他不想在我输入的坐标处旋转。我究竟做错了什么?

这是我的代码:

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 坐标,你就会尝试翻转他,但他可能永远不会到达那个确切的坐标。 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;
}
}

关于c# - 在某个x坐标Unity旋转头像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44308088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com