简体   繁体   English

当我将角色编码设置为0度时,为什么我的角色以45度角生成?

[英]Why is my character spawning at a 45 degree angle when I've coded it to be set to 0 degrees?

The title describes my problem, as you can see here my character is spawning at a 45 degree angle. 标题描述了我的问题,正如您在此处看到的那样我的角色以45度角生成。 The character always spawns at (0, 0) 角色始终在(0,0)处生成

I have created some code for animations that uses the character's position and desired position to calculate at what angle the character should be at. 我已经为动画创建了一些代码,该代码使用角色的位置和所需的位置来计算角色应处于的角度。 This angle is determined by using the percentage of distance the character has travelled to its destination (these destinations are only Y values. X is increased by 1 every tick). 该角度是通过角色到其目标的距离的百分比来确定的(这些目标只是Y值。X每隔1分便增加1)。 I have made an equation to determine the angle based on the percentage travelled. 我做了一个方程,根据行进的百分比确定角度。 The equation: -(9/500)X^2 + (9/5)X. 等式:-( 9/500)X ^ 2 +(9/5)X。 This is a parabola that intersects point (0, 0), (50, 45) ← 45 degree angle and (100, 0). 这是一个与点(0,0),(50,45)←45度角和(100,0)相交的抛物线。

The code I made to use this equation is as follows: 我使用此方程式编写的代码如下:

float GetRotationDegrees(int Current, int Target, int LaneWidth) {
    int numerator = Current - LaneWidth;
    int denominator = Target - LaneWidth;
    float X;
    if (denominator == 0) X = 0;
    else X = abs(numerator / denominator * 100);
    return -(9/500)*pow(X,2)+(9/5)*X; // function: -(9/500)X^2 + (9/5)X.
}

The LaneWidth is always 400, Current is the character's current Y coordinate and Target is the target Y coordinate. LaneWidth始终为400,Current是角色的当前Y坐标,Target是目标Y坐标。 At Y = 0 the equation should be -400 / -400 * 100 = 100. And when you put 100 in the equation you get 0, this value of 0 is then used like this: 在Y = 0时,等式应为-400 / -400 * 100 =100。当您在等式中输入100时,您将得到0,则该值0如下使用:

SetActorLocation(FVector(1, sin(angle * PI / 180), 0).Rotation());

I am really confused right now because whenever I manually try this it won't work. 我现在真的很困惑,因为每当我手动尝试此操作都不会起作用。

It is probably something stupid I did, I tend to look over things a lot of the time and am not that good at maths (I am also new to C++, so the mistake might be there too). 这可能是我做的愚蠢的事情,我经常花很多时间看书,而且数学也不是那么好(我对C ++还是陌生的,所以错误也可能在那里)。 If someone could help me I would really appreciate it! 如果有人可以帮助我,我将不胜感激!

(By the way, I know that the way that GetRotationDegrees decides X the angle will only return positive angles, I want to get this to work first) (顺便说一句,我知道GetRotationDegrees决定X角度的方式只会返回正角,我想让它首先工作)

Fixed it with this: 修复此问题:

float GetRotationDegrees(int Current, int Target, int LaneWidth) {
    if (Current == Target) return 0;
    int MovingFrom;
    if (Target != 0) MovingFrom = 0;
    else if (Target > Current) MovingFrom = -LaneWidth;
    else MovingFrom = LaneWidth;

    float Percentage = float(Current - MovingFrom) / float(Target - MovingFrom) * 100;
    if (Target < Current) Percentage = -Percentage;
    bool isNegative = Percentage < 0;
    if (isNegative) return -(-9 * pow(abs(Percentage), 2) / 500 + 9 * abs(Percentage) / 5);
    return -9 * pow(Percentage, 2) / 500 + 9 * Percentage / 5;
}

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

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