0
\$\begingroup\$

I have created a simple player control. I have used "Vertical" and "Horizontal" axes as input to move. There is a character controller and I used this to move my player. But I don't know how to add a force as jump with space input. I am new to game development. Please help me to learn this. This is my script,

public class PlayerControll : MonoBehaviour { public Animator anim; public CharacterController characterController; public Rigidbody rb; public float Speed; public float Direction; Vector3 moveDir = Vector3.zero; float speedwalking = 8; float gravity = 8; void Start() { anim = GetComponent<Animator>(); characterController = GetComponent<CharacterController>(); rb = GetComponent<Rigidbody>(); } void Update() { if (characterController.isGrounded) { anim.SetBool("Grounded", true); if (Input.GetButton("Vertical") || Input.GetButton("Horizontal")) { Speed = Input.GetAxis("Vertical"); Direction = Input.GetAxis("Horizontal"); anim.SetFloat("Speed", Speed); anim.SetFloat("Direction", Direction); moveDir = new Vector3(Direction, 0, Speed); moveDir *= speedwalking; moveDir = transform.TransformDirection(moveDir); characterController.Move(moveDir * Time.deltaTime); moveDir.y -= gravity * Time.deltaTime; } else { anim.SetFloat("Speed", 0); anim.SetFloat("Direction", 0); } } else { anim.SetBool("Grounded", false); } } } 
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Please search before posting a new question in the future. The answer to your question is right in the CharacterController.Move API reference.

You can do this by adding a movement in the opposite direction of your gravity when the Space key is pressed. I also moved the gravity calculation and the applying of the movement vector.

public class PlayerControll : MonoBehaviour { public Animator anim; public CharacterController characterController; public Rigidbody rb; public float Speed; public float JumpSpeed = 8.0f; // new public float Direction; Vector3 moveDir = Vector3.zero; float speedwalking = 8; float gravity = 8; void Start() { anim = GetComponent<Animator>(); characterController = GetComponent<CharacterController>(); rb = GetComponent<Rigidbody>(); } void Update() { if (characterController.isGrounded) { anim.SetBool("Grounded", true); if (Input.GetButton("Vertical") || Input.GetButton("Horizontal")) { Speed = Input.GetAxis("Vertical"); Direction = Input.GetAxis("Horizontal"); anim.SetFloat("Speed", Speed); anim.SetFloat("Direction", Direction); moveDir = new Vector3(Direction, 0, Speed); moveDir *= speedwalking; moveDir = transform.TransformDirection(moveDir); if(Input.GetButton("Jump")) // new { moveDir.y = JumpSpeed; } } else { anim.SetFloat("Speed", 0); anim.SetFloat("Direction", 0); } } else { anim.SetBool("Grounded", false); moveDir = Vector3.zero; // reset all movement } moveDir.y -= gravity * Time.deltaTime; // moved characterController.Move(moveDir * Time.deltaTime); } } 

Note that this requires "Jump" to be defined in the InputManager.

\$\endgroup\$
3
  • \$\begingroup\$ That worked brother.But Now after I pressing the button, it is keep moving.it is not stopping.Can you solve this either? \$\endgroup\$ Commented Apr 24, 2020 at 6:51
  • \$\begingroup\$ If you mean that after jumping, the player is continuing to go in the same direction, then you can reset the moveDir in your second else block: moveDir = Vector3.zero; \$\endgroup\$ Commented Apr 24, 2020 at 6:56
  • \$\begingroup\$ @JeewanthaLahiru You are showing some symptoms of help vampirism. You might be able to become a better developer more quickly if you don't just ask people to give you the code which solves your problems for you but if you would instead seek to understand why your problems occur and how you could solve them yourself. \$\endgroup\$ Commented Apr 24, 2020 at 10:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.