public class PlayerMovement : MonoBehaviour
{
#region Variables
private float currentSpeed;
private float currentJumpForce;
public float walkSpeed = 20f;
public float runSpeed = 35f;
public float walkJumpForce = 40f;
public float runJumpForce = 50f;
public float gravity = 130f;
public Vector3 moveDir = Vector3.zero;
#endregion
CharacterController controller;
private void Start()
{
controller = gameObject.GetComponent<CharacterController>();
}
void Update()
{
if (Input.GetButton("Run"))
{
currentSpeed = runSpeed;
currentJumpForce = runJumpForce;
}
else
{
currentSpeed = walkSpeed;
currentJumpForce = walkJumpForce;
}
if (controller.isGrounded)
{
moveDir = new Vector3(Input.GetAxis("Right"), 0, Input.GetAxis("Up"));
moveDir = new Vector3(Input.GetAxis("Left"), 0, Input.GetAxis("Down"));
moveDir = Vector3.ClampMagnitude(moveDir, 1);
moveDir = transform.TransformDirection(moveDir);
moveDir *= currentSpeed;
if (Input.GetButtonDown("Jump"))
{
moveDir.y = currentJumpForce;
}
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
}
Hey guys. We have a character controller and we are making a script to move the character controller. We are not having any problems with the jump, but the only problem we are having is with the moving.
We can’t get both of those lines of code to work at once. If we delete one, the other one works, but if we delete the other, then the other one works. If we keep both lines of code, then only the one on the bottom works. How do we get both lines of code to work at the same time?