1
votes

I'm Making a movement script with vector 2 and Rigidbody.velocity. I want my gameobject to move until a certain x pos and then return to the initial pos, all of that while moving continiously. I achieved the first part, it goes backwards when it reaches a certain point but then the backwards movement never stops:

public float spawnPos;
public float currentPos;
public float constantSpeed = 3;
public float speed = 0;
KeyInput scriptBeholderKI;
Rigidbody2D squadRigidBody;

//Comprovation of spawnposition and KeyInput
//Script adquisition
void Start () {

    spawnPos = transform.position.x;
    scriptBeholderKI = gameObject.GetComponent <KeyInput> ();
    squadRigidBody = gameObject.GetComponent <Rigidbody2D> ();

}

void FixedUpdate () {

    //Movement
    squadRigidBody.velocity = new Vector2 (constantSpeed + speed , 0);

    //key inputs
    if (Input.GetKeyDown (scriptBeholderKI.forward)) {   

        StopAllCoroutines ();
        StartCoroutine (RightMovement(0f));
    }

    if (Input.GetKeyDown (scriptBeholderKI.backwards)) {

        StopAllCoroutines ();
        StartCoroutine (LeftMovement (0f));
    }
}

//Speed values (Right, Left)
IEnumerator RightMovement (float Rloop) {

    while (transform.position.x < constantSpeed * Time.time + spawnPos + 14) {

        speed = 10f;
        yield return new WaitForSeconds (Rloop);
    }

    if (transform.position.x> constantSpeed * Time.time + spawnPos + 14) {

        StopAllCoroutines ();
        StartCoroutine (LeftMovement (0f));
    }
}

IEnumerator LeftMovement (float Lloop) {

    while (transform.position.x > constantSpeed * Time.time + spawnPos) {

        speed = -7f;
        yield return new WaitForSeconds (Lloop);
    }
}

If you also see anything that could be improved pls make me note c:

edit:

when I press the forward input my gameobject moves until it reaches a point, when I press the bacwards one it moves to the left nonstop.

also the thing is that I want my target to move at the constantspeed of the gameobject, thats why I use Time.time. the movements I do with the inputs are more properly said accelerations. I want my target to be always at the same relative distance from the gameobject, since the game starts. At least the maxium reach to the right is the one I want...

And I also tried to remove the stop all coroutines block and nothing, I but my intention was to be able to cancel the movement whenever I want (I removed the one in the RightMovement coroutine, however. It wasn't neecesary after all)

one last thing: I changed "FixedUpdate" for "Update" because it gave me problems with my inputs response, I don't know if it's relevent but just in case.

1
id say this this doesnt do what you think it does transform.position.x < constantSpeed * Time.time + spawnPos + 14. from the unity docs -> "(Time.time is) The time at the beginning of this frame (Read Only). This is the time in seconds since the start of the game." instead, when you start to move, calculate your target, store it and check against this position, when turning around, do the same. - yes
I can already see anything that should be change. Can you edit your question and add what happens when the forward key is pressed and what happens when the backward key is pressed. - Programmer
Why don't you create just a "Movement" function and pass in 'float' or '-float' depending on which way you want to move? As it looks like the distance you want to travel is fix... (or a bool, e.g. if (doMoveLeft) mvSpeed *= -1; [movement code])? Also, did you try to debug.log the value evaluated in your "while" expression? What makes you think LeftMov... will stop if it lacks the "StopAllCoroutines" block? - user2299169

1 Answers

0
votes

I think you are mistaking the Time.time variable. Just like yes mentions in his comment

Time.time is the time in seconds since the start of the game.

This means, as time moves by, so will your target, which will slowly but surely move away.

Instead of Time.time you could however use Time.deltaTime

Time.deltaTime is the time in seconds it took to complete the last frame

This is most often used for movement.

When you multiply with Time.deltaTime you essentially express: I want to move this object 10 meters per second instead of 10 meters per frame, if the multiplication is 10.

Unity docs: Time.deltaTime