Search code examples
c#unity-game-engineqvector3d

How can I move my object based on my axis direction at all times?


I'm trying to move my ball to a fixed distance. For that I used some Vector3 and Lerping.

But when when my object rotates, the ball still move in the same direction and not in the new direction set by my X axis direction. I have added more details in what I want to be accomplished in the comments of the code.

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;
        }
    }
}

Solution

  • when you say

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

    you're declaring a vector pointing to the right (yes, (1, 0, 0) is right, not forward) in the world space, unity will use that little axis at the upper right in the scene view as reference to apply your movement,

    which will not change when you rotate.

    what you want to do is use the GameObject's transform as reference, this is a local space coordinate that takes the rotation in consideration, instead of

    myVector = Vector3.forward;
    

    try

    myVector = transform.forward;
    

    and apply movement on that direction.