how to combine input from keyboard and transform.right relative to a Game Object movement

private float moveDirection = 0.0f;

public float moveSpeeds;

public float moveDuration;

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis(“Horizontal”);

    float moveVertical = Input.GetAxis("Vertical");
    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    GetComponent<Rigidbody>().velocity.x = movement.x * speed +movement.x * moveDirection;
    GetComponent<Rigidbody>().velocity.y = 0.0f
    GetComponent<Rigidbody>().velocity.z = movement.z * speed;

}

public void MoveRight()
{

    moveDirection =     +moveSpeeds;

    while (true)
    {
        if (Time.time > moveDuration)
        {
            break;
        }
    }
    moveDirection = 0.0f;

Similar for left only with - instead of +.

What I try to do is while some situation has just happened and for å short amount of time after the player ship in an arcade shooter game should be pushed either to the right or to the left depending on which side the ship hit, but that is the background story, the problem is that writing GetComponent().velocity.x is illegal, is it so simple that I am missing a word between velocity and x? and if that is not the problem could someone please tell me how I can combine input from the keyboard with for eksample transform.right, everything I have tried have made my game crash and I could not find anything relevant in Unity answers, thank you!

Hello @hemingso

You can not access the velocity of an object directly, you need to access rigidbody of an object first.

eg: gameObject.GetComponent().velocity = new Vector3(0,10,0);

Go through this official unity link, if its needed:

Thanks
Ankush Taneja

Thanks for the help, but it still don’t functions, previously the game crashed every time the player touched an enemy, now it only crashes if I set moveDuration to somewhere greater than 1 and if I set the moveSpeeds to 5 times the normal player movement, it should be pushed either to the left or to the right now everything happens as if MoveRight() and MoveLeft() was not there even though they are called from the same function one after another, after the function dealing damage to enemy and player which functions as it should, and I have called them in the exact same way and have tested to turn the greater than symbol with no effect in the function calling MoveLeft() and MoveRight() as in

public Transform target //target is the player ship.

if (target.transform.position.x < this.transform.position.x)

and the code below have worked many times.

It can look like the variable moveDirection always is set to 0.0f in the FixedUpdate function without being updated. If you have any suggestions of how moveDirection could be updated in FixedUpdate or if there is something else which is wrong which prevents the player ship from being pushed and why the game still crashes if the moveDuration is too high, please explain. (Could it possibly be something with Start() or Awake()?)

@hemingso Just from the example in your original post I can find many things that are potentially wrong (I don’t really know what you’re aiming for so I can’t be sure).

  1. moveDirection = +moveSpeeds; should probably be moveDirection += moveSpeeds;

  2. if (Time.time > moveDuration) Time.time is the time in seconds since the game started. It will most likely ALWAYS be greater than moveDuration. What you probably want to do is use a Coroutine() to decrease your moveDuration-variable or decrease it by subtracting Time.deltaTime every frame (in the Update()-function).

  3. while (true) This is just not going to work. What you want to use is a Coroutine (look above). When you loop something with a while-loop it will continue on looping until it is done. Remember that code is read in a linear matter (line-by-line) so the rest of the game will freeze during this loop (it won’t read any other code) (Note: This is not the case when using multiple threads). What you want to use is a Coroutine which does break out of the loop by itself when the frame is over and returns to the loop the next frame.

In code it would look something like this:

 public IEnumerator MoveRight(int duration)
 {
     moveDirection += moveSpeeds;
     while (duration > 0) //loop as long as the duration is greater than 0
     {
         yield return new WaitForSeconds(1f); //This line is very important, it tells the engine to break out of the loop and return to it after 1 second
         duration--; //when one second have passed you subtract 1 from duration. This will get us one step closer to end the loop.
     }
     moveDirection = 0.0f;
}

And you would call this function using: StartCoroutine(MoveRight([your duration]);