- Home /
Character walks left and right with axis horizontal, but when press Fire1 it should run but don't works correctly
My character should walk left and right with axis horizontal, this works perfectly. But now I want, that my character runs, when I press additionally "Fire1". This works partly too, but when I change the direction while pressing "Fire1" the character stops. Without pressing "Fire1" the direction change works.
//If on platform
if (onPlatform) {
activeMoveSpeed = moveSpeed * onPlatformSpeedModifier;
} else if(Input.GetButton("Fire1")) {
activeMoveSpeed = moveSpeed * sprintSpeedModifier;
} else {
activeMoveSpeed = moveSpeed;
}
Debug.Log (Input.GetAxisRaw ("Horizontal"));
//Player go left and right
if (Input.GetAxisRaw ("Horizontal") > 0f) {
myRigidbody.velocity = new Vector3 (activeMoveSpeed, myRigidbody.velocity.y, 0f);
transform.localScale = new Vector3 (1f, 1f, 1f);
} else if (Input.GetAxisRaw ("Horizontal") < 0f) {
myRigidbody.velocity = new Vector3 (-activeMoveSpeed, myRigidbody.velocity.y, 0f);
transform.localScale = new Vector3 (-1f, 1f, 1f);
} else {
myRigidbody.velocity = new Vector3(0f, myRigidbody.velocity.y, 0f);
}
Answer by ivanparas · Jan 31, 2017 at 09:15 PM
You should always make the modifiers apply to the player's speed and change the value of the modifiers based on whether the player in on the platform or the mouse is held down. This will give you more consistent behavior. This makes it so the playing being on a platform or the mouse1 being down don't interfere with each other.
if(onPlatform)
{
onPlatformSpeed = 3.0f; //your platform speed value here
}
else
{
onPlatformSpeed = 1.0f; //make it 1 so it doesn't affect the normal speed
}
if(Input.GetButton("Fire1"))
{
sprintSpeed = 3.0f; //your platform speed value here
}
else
{
sprintSpeed = 1.0f; //make it 1 so it doesn't affect the normal speed
}
activeMoveSpeed = moveSpeed * sprintSpeed * onPlatformSpeed;
Thanks for the help :) I will try this and let you know if it solves my problem. Thanks :)
Hmm, poorly it don't change the behavior, that the character stops, when I change the direction while I press "Fire1"
Answer by MarshallN · Feb 03, 2017 at 08:42 PM
Something I notice is that you are only checking for sprint speed if the player is not on a platform - in other words, the only time your script cares about "Fire1" being pressed is when you aren't on a platform. The "else if" makes that happen. @ivanparas's answer fixes that, so if you've already fixed it, don't worry about it.
Also, you can clean up your movement a little bit by having something like
float moveSpeed = activeMoveSpeed * Input.GetAxisRaw("Horizontal");
myRigidBody.velocity = new Vector3( moveSpeed, myRigidBody.velocity.y, 0f); // Set movement speed based on activeMoveSpeed and the input.
if(moveSpeed > 0f)
{
transform.localScale = new Vector3(1f, 1f, 1f);
}
else if (moveSpeed < 0f)
{
transform.localScale = new Vector3(-1f, 1f, 1f); // Make the character look to the left if horizontal movement is negative.
}