Why is the script not working (function not changing value)
I have this script that I made for an FPS character. 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. 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.
How can I fix this, and if anyone can explain why this happens, it would be appreciated? Does this have to do with he value not being passed to the Update function?
Your answer
Follow this Question
Related Questions
Jump button doesn't work 1 Answer
How can I start a Coroutine with the onClick of a button? 0 Answers
How to change a button image with script? 1 Answer
Make button move character? 1 Answer