c# - 如何始终根据我的轴方向移动我的对象?

标签 c# unity3d qvector3d

我正在尝试将我的球移动到固定距离。 为此,我使用了一些 Vector3 和 Lerping。

但是当我的对象旋转时,球仍然沿相同的方向移动,而不是沿我的 X 轴方向设置的新方向移动。我在代码的注释中添加了更多我想要完成的细节。

public float timeTakenDuringLerp = 1f;

/// <summary>
/// How far the object should move when 'UpArrow' is pressed
/// </summary>
public float distanceToMove = 7; //value can be change in unity

//Whether we are currently interpolating or not
private bool _isLerping;

//The start and finish positions for the interpolation
private Vector3 _startPosition;
private Vector3 _endPosition;

//The Time.time value when we started the interpolation
private float _timeStartedLerping;
Vector3 myVector;

/// <summary>
/// Called to begin the linear interpolation
/// </summary>

void StartLerping1()
{
    _isLerping = true;
    _timeStartedLerping = Time.time;

    //We set the start position to the current position, and the finish to 7 spaces in the 'forward' direction
    _startPosition = transform.position;

    myVector = new Vector3(1, 0, 0);
    _endPosition = transform.position + myVector * distanceToMove;
}

void Update()
{
    //When the user hits the up arrow, we start lerping
    if (Input.GetKey(KeyCode.UpArrow))
    {
        StartLerping1();
    }

    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        // THE ROTATION IS SUPPOSED TO HAPPEN, AND THE MOVEMENT SHOULD BE BASED ON THIS NEW DIRECTION
    }

}


//We do the actual interpolation in FixedUpdate(), since we're dealing with a rigidbody
    void FixedUpdate()
    {
        if (_isLerping)
        {
            //We want percentage = 0.0 when Time.time = _timeStartedLerping
            //and percentage = 1.0 when Time.time = _timeStartedLerping + timeTakenDuringLerp
            //In other words, we want to know what percentage of "timeTakenDuringLerp" the value
            //"Time.time - _timeStartedLerping" is.
            float timeSinceStarted = Time.time - _timeStartedLerping;
            float percentageComplete = timeSinceStarted / timeTakenDuringLerp;
        //Perform the actual lerping.  Notice that the first two parameters will always be the same
        //throughout a single lerp-processs (ie. they won't change until we hit the space-bar again
        //to start another lerp)
        transform.position = Vector3.Lerp(_startPosition, _endPosition, percentageComplete);

        //When we've completed the lerp, we set _isLerping to false
        if (percentageComplete >= 1.0f)
        {
            _isLerping = false;
        }
    }
}

最佳答案

当你说

myVector = new Vector3(1, 0, 0); //you can short hand to myVector = Vector3.right

你在世界空间中声明了一个指向右侧的向量(是的,(1, 0, 0) 是正确的,而不是向前),unity 将使用场景 View 中右上角的那个小轴作为引用运用你的 Action ,

旋转时不会改变。

你想要做的是使用游戏对象的变换作为引用,这是一个考虑了旋转的局部空间坐标,而不是

myVector = Vector3.forward;

尝试

myVector = transform.forward;

并在该方向上应用移动。

关于c# - 如何始终根据我的轴方向移动我的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54895992/

相关文章:

c# - XUnit 和 ITestOutputHelper 实例化

c# - 如何在文本框中显示发布版本?

c# - 来自 C# 的 crypto-js DES 解密

c++ - 使用 QVector3D 在 OpenGL 中绘制点

unity-game-engine - 计算盒子的边界,包括旋转

c++ - 如何在 Qt 中声明和使用二维整数数组(GUI)?

c# - 关于接口(interface)

c# - 输入节奏游戏

python - 是否有官方/非官方的方式在 python 中编写 Unity 脚本?

c# - Unity 中的按钮,不使用 UI?