I have this script that I made for an FPS character.
public class Player_Movement : MonoBehaviour
{ public CharacterController controller; public Transform camera;
public float gravity = -9.81f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool grounded;
public GameObject joystick;
public bool isMoving;
public float speed = 12f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
public float jumpHeight;
public Vector3 yPosition;
public bool jumpAble = true;
// Update is called once per frame
void FixedUpdate()
{
grounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (grounded)
{
yPosition.y = -2f;
jumpAble = true;
}
Vector3 direction = new Vector3(joystick.GetComponent<Joystick>().inputDirection.x, 0f, joystick.GetComponent<Joystick>().inputDirection.y);
isMoving = direction.magnitude != 0 ? true : false;
if (isMoving)
{
controller.Move(direction * speed * Time.deltaTime);
}
yPosition.y += gravity * Time.deltaTime;
UnityEngine.Debug.Log(yPosition);
controller.Move(yPosition * Time.deltaTime);
}
public Vector3 Jump()
{
UnityEngine.Debug.Log(jumpAble);
UnityEngine.Debug.Log(grounded);
if (jumpAble && grounded)
{
jumpAble = false;
yPosition.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
UnityEngine.Debug.Log(yPosition.y);
UnityEngine.Debug.Log(yPosition);
return yPosition;
}
return yPosition;
}
}
The game operates on mobile touch controls, and the Jump() is called when the jump button is pressed. However, the character does not jump when I press the button, even though the function is being called, and the yPoisition is being changed in the function. I know that changing the yPosition should make the player jump, as when I add this code to the Update:
if (Input.GetKey(KeyCode.Space) && jumpAble)
{
fallDown.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
Everything works fine. Why is the new yPoisition value not being passed to Update, and why is this happening?
yPosition.yback to -2? \$\endgroup\$