I updated my code, my current problem is just that the movement of the player doesn't work anymore and I would like to know what I have to do so that I can adjust the height of the jump and that when this height is reached, gravity comes through again Code:
//Private Variables
private CharacterController _chaCont;
private float currentGravity;
private Vector3 finalMovement;
//Globally delared
//Public Variables
public float speed;
public float gravity;
public float jumpSpeed;
// Start is called before the first frame update
void Start()
{
_chaCont = gameObject.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
finalMovement = jump() + ApplyGravity();
}
_chaCont.Move(finalMovement * Time.deltaTime);
}
Vector3 ApplyGravity()
{
Vector3 gravityMovement = new Vector3(0, -currentGravity, 0);
currentGravity += gravity * Time.deltaTime;
if (_chaCont.isGrounded)
{
if (currentGravity > 1f)
currentGravity = 1f;
}
return gravityMovement;
}
Vector3 Movement()
{
Vector3 moveVector = Vector3.zero;
moveVector += transform.forward * Input.GetAxis("Vertical");
moveVector += transform.right * Input.GetAxis("Horizontal");
moveVector *= speed;
return moveVector;
}
Vector3 jump()
{
Vector3 jumpVector = Vector3.zero;
jumpVector += transform.up * Input.GetAxis("Jump");
jumpVector *= jumpSpeed;
return jumpVector;
}
}
Good Night. Have you tried the input system C# provides should be like below code.
//Private Variables
private CharacterController _chaCont;
private float currentGravity;
private Vector3 finalMovement; //Globally delared
//Public Variables
public float speed;
public float gravity;
// Start is called before the first frame update
void Start()
{
_chaCont = gameObject.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Vector3 initialMovement = Movement();
if(Input.GetKeyDown(KeyCode.Space)){
finalMovement = initialMovement + ApplyGravity();
}
else{
finalMovement = initialMovement;
}
_chaCont.Move(finalMovement * Time.deltaTime);
}
Vector3 ApplyGravity()
{
Vector3 gravityMovement = new Vector3(0, -currentGravity, 0);
currentGravity += gravity * Time.deltaTime;
if (_chaCont.isGrounded)
{
if (currentGravity > 1f)
currentGravity = 1f;
}
return gravityMovement;
}
Vector3 Movement()
{
Vector3 moveVector = Vector3.zero;
moveVector += transform.forward * Input.GetAxis("Vertical");
moveVector += transform.right * Input.GetAxis("Horizontal");
moveVector *= speed;
return moveVector;
}