- Home /
How to get our character controller script to make our player move?
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?
Answer by seckincengiz · Jun 17, 2018 at 08:30 AM
Add some conditions.
if (Input.GetAxis("Horizontal") > 0.3f)
{
//Move Right
}
if (Input.GetAxis("Horizantal") < 0.3f)
{
//Move Left
}
Your answer
Follow this Question
Related Questions
3D Character Controller slowing down the higher the slope angle (both up and down) 1 Answer
C# Very Basic Character Controller Script 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
Need helping getting a character controller to jump 1 Answer
Camera wil not move between given points, stays at first given point 1 Answer