# Only one key at a time working

**URL:** https://discussions.unity.com/t/only-one-key-at-a-time-working/508658
**Category:** Input
**Tags:** Scripting
**Created:** [July 1, 2013, 6:15pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658 "2013-07-01T18:15:48Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Koramere](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Koramere](https://discussions.unity.com/u/Koramere)
#### Post date: [July 1, 2013, 6:15pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/1 "2013-07-01T18:15:48Z")

</div>

Hi,

I’m working on my first game project with unity. I started by creating the character controller. The basic movement logic is as follows:

if (Input.GetKey(“w”)) // Cheching for the “W” key.  
{  
rigidbody.MovePosition(transform.position + playerForwardSpeed);  
}  
if (Input.GetKey(“s”)) // Cheching for the “S” key.  
{  
rigidbody.MovePosition(transform.position - playerForwardSpeed);  
}  
if (Input.GetKey(“a”)) // Cheching for the “A” key.  
{  
rigidbody.MovePosition(transform.position - playerSidewaysSpeed);  
}  
if (Input.GetKey(“d”)) // Cheching for the “D” key.  
{  
rigidbody.MovePosition(transform.position + playerSidewaysSpeed);  
}

So when I’m playing the game if I’m pressing multiple keys at once, the one that was pressed first is the only one that will work. Any Ideas why?

Thanks

---

<div class="post-metadata">

### Author: ![Eiznek](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Eiznek](https://discussions.unity.com/u/Eiznek)
#### Post date: [July 1, 2013, 6:57pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/2 "2013-07-01T18:57:16Z")

</div>

Something you could try.

```auto
Vector3 totalMovement = new Vector3();
if (Input.GetKey("w")) // Cheching for the "W" key.
{
	totalMovement += playerForwardSpeed;	
}
if (Input.GetKey("s")) // Cheching for the "S" key.
{
	totalMovement -= playerForwardSpeed;
}
if (Input.GetKey("a")) // Cheching for the "A" key.
{	
	totalMovement -= playerSidewaysSpeed;
}
if (Input.GetKey("d")) // Cheching for the "D" key.
{
	totalMovement += playerSidewaysSpeed;
}

// If we actually have some moment go ahead and apply it
if(!totalMovement.Equals(new Vector3()))
{
	rigidbody.MovePosition(transform.position + totalMovement);
}

```

---

<div class="post-metadata">

### Author: ![Koramere](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Koramere](https://discussions.unity.com/u/Koramere)
#### Post date: [July 1, 2013, 7:42pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/3 "2013-07-01T19:42:24Z")

</div>

Thanks. Actually I did try it in my script and I’m afraid it didn’t work too well. i appreciate it anyway though.

---

<div class="post-metadata">

### Author: ![xniinja](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/xniinja/32/551662_2.png) [@xniinja](https://discussions.unity.com/u/xniinja)
#### Post date: [July 1, 2013, 7:48pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/4 "2013-07-01T19:48:27Z")

</div>

Have you put in debug markers in to see if they all get hit?

---

<div class="post-metadata">

### Author: ![Eiznek](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Eiznek](https://discussions.unity.com/u/Eiznek)
#### Post date: [July 1, 2013, 7:58pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/5 "2013-07-01T19:58:53Z")

</div>

Worked fine for me

 ![1287716--58691--$ss_works.png](https://europe1.discourse-cdn.com/unity/original/4X/9/d/6/9d653375daf4b4ae839a3bd0f6e7657bfae660dd.png)

[https://dl.dropboxusercontent.com/u/32175794/ss\_works.png](https://dl.dropboxusercontent.com/u/32175794/ss_works.png)

```auto
 public void TrySomething()
    {
        Vector3 playerForwardSpeed = new Vector3(0, 0, 1);
        Vector3 playerSidewaysSpeed = new Vector3(0, 1, 0);

        Vector3 totalMovement = new Vector3();
        if (Input.GetKey("w")) // Cheching for the "W" key.
        {
            totalMovement += playerForwardSpeed;
        }
        if (Input.GetKey("s")) // Cheching for the "S" key.
        {
            totalMovement -= playerForwardSpeed;
        }
        if (Input.GetKey("a")) // Cheching for the "A" key.
        {
            totalMovement -= playerSidewaysSpeed;
        }
        if (Input.GetKey("d")) // Cheching for the "D" key.
        {
            totalMovement += playerSidewaysSpeed;
        }

        // If we actually have some moment go ahead and apply it
        if (!totalMovement.Equals(new Vector3()))
        {
            Debug.Log(totalMovement);
        }
    }

    public void Update()
    {
        TrySomething();
     }

```

I can’t seem to attach the ss directly… So I added the link.

---

<div class="post-metadata">

### Author: ![Koramere](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Koramere](https://discussions.unity.com/u/Koramere)
#### Post date: [July 1, 2013, 8:11pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/6 "2013-07-01T20:11:06Z")

</div>

I appreciate it, but with your code I generally tend to go strait through the collision mesh of the environment for example. Am I doing something wrong?

---

<div class="post-metadata">

### Author: ![Eiznek](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Eiznek](https://discussions.unity.com/u/Eiznek)
#### Post date: [July 1, 2013, 8:15pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/7 "2013-07-01T20:15:12Z")

</div>

Do you have a collider attached to the object your moving? Rigidbody is one thing, but you still need a collider on the object itself.

---

<div class="post-metadata">

### Author: ![xniinja](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/xniinja/32/551662_2.png) [@xniinja](https://discussions.unity.com/u/xniinja)
#### Post date: [July 1, 2013, 8:22pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/8 "2013-07-01T20:22:59Z")

</div>

Also, if you are colliding two convex collision meshes Unity doesn’t really like that.

---

<div class="post-metadata">

### Author: ![Koramere](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Koramere](https://discussions.unity.com/u/Koramere)
#### Post date: [July 1, 2013, 8:54pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/9 "2013-07-01T20:54:43Z")

</div>

I have collision meshes on both my character and my environment. And I like using rigidbody.MovePosition in general. If you don’t mind my prefference would be to use a fixed version of it. Thanks a bunch.

---

<div class="post-metadata">

### Author: ![xniinja](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/xniinja/32/551662_2.png) [@xniinja](https://discussions.unity.com/u/xniinja)
#### Post date: [July 1, 2013, 10:12pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/10 "2013-07-01T22:12:49Z")

</div>

Oh, I take that back. Mesh colliders can’t collide with each other if they are both concave colliders. To change this check the convex box on the Mesh collider menu. This will make the collider convex which is easier to calculate, and will therefore work.

> **[Unity - Manual: Mesh collider component reference](https://docs.unity3d.com/Manual/class-MeshCollider.html)**

---

<div class="post-metadata">

### Author: ![Koramere](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Koramere](https://discussions.unity.com/u/Koramere)
#### Post date: [July 4, 2013, 4:05pm UTC](https://discussions.unity.com/t/only-one-key-at-a-time-working/508658/11 "2013-07-04T16:05:45Z")

</div>

OK I’ve solved the problem. Thanks.
