Sometimes my player jumps relatively high, but then he jumps relatively low. I tried several methods, but none of them worked.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody rb;
public GameObject cam1;
public Transform playerBody;
RaycastHit hit;
RaycastHit hitGround;
public float MoveForce = 10f;
public float jumppadForce = 10f;
public bool hasKey;
public bool isJumping;
private void Update()
{
if(Physics.Raycast(cam1.transform.position, cam1.transform.forward, out hit, 4))
{
if (Input.GetMouseButtonDown(0))
{
switch (hit.transform.gameObject.tag)
{
case "key":
Destroy(hit.transform.gameObject);
hasKey = true;
break;
case "lock":
if (hasKey)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
break;
}
}
if (Input.GetMouseButton(0))
{
switch (hit.transform.gameObject.tag)
{
case "movableJumppad":
hit.transform.SetParent(playerBody);
break;
}
}
if (Input.GetMouseButtonUp(0))
{
switch (hit.transform.gameObject.tag)
{
case "movableJumppad":
hit.transform.SetParent(null);
break;
}
}
}
// part of jumping mechanic
if(Physics.Raycast(transform.position, Vector3.down, out hitGround, 1.2f))
{
switch (hitGround.transform.gameObject.tag)
{
case "jumppad":
isJumping = false;
break;
case "movableJumppad":
isJumping = false;
break;
}
}
else
{
isJumping = true;
}
}
// Update is called once per frame
void FixedUpdate()
{
// jumping mechanic
if (!isJumping)
{
rb.AddForce(jumppadForce * Vector3.up * Time.fixedDeltaTime, ForceMode.Impulse);
}
transform.Translate(Input.GetAxis("Vertical") * Time.deltaTime * MoveForce * Vector3.forward);
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * MoveForce * Vector3.right);
}
}