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
Something you could try.
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);
}
Thanks. Actually I did try it in my script and I’m afraid it didn’t work too well. i appreciate it anyway though.
Have you put in debug markers in to see if they all get hit?
Worked fine for me
https://dl.dropboxusercontent.com/u/32175794/ss_works.png
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.
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?
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.
Also, if you are colliding two convex collision meshes Unity doesn’t really like that.
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.
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.
OK I’ve solved the problem. Thanks.