I feel like this is something stupid but i cant figure out whats going wrong with it. My character instantly jumps to peak height(should be a slow jump), then falls slowly as should be. ive tried to put this chunk in both the update and fixed update, same result. i also tried making a new scene with just cube and plane and it still jumps the same. any help would be greatly appreciated, thank you.
Im up for other methods of making my character jump too if this is bad for some reason(imported it from old .js code i was using). Im controlling a clone as a main character and the rigid body and velocity thing isn’t working on it either(will not jump at all).
//Jump
if (Input.GetButton("Jump"))
{
aa = transform.position;
if (aa.y <= 1)
{
bb = transform.position;
while (bb.y <= (aa.y + 4))
{
transform.Translate(0, (.25f * Time.deltaTime), 0);
bb = transform.position;
}
}
}
If you have while, for or foreach loop it will run completely before code can continue. In your case code will be stuck in while until (bb.y <= (aa.y + 4)) becomes true making your character to instantly move to max height.
You said that he falls slowly which sounds like you are using physics and rigidbody? If this is the case then you could just use force something like this. I didn’t test any of these examples, but they should at least give you the idea how to solve your problem.
public float jumpForce;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (Input.GetButton("Jump"))
{
rb.AddForce(transform.up * jumpForce);
}
}
If for some reason you want or need to use Translate then you can use coroutine or just do it in update without any loops. In Update something like this should work.
if(isJumping)
{
if(bb.y <= (aa.y + 4))
{
transform.Translate(0, (.25f * Time.deltaTime), 0);
bb = transform.position;
}
else
{
isJumping = false;
}
}
else if (Input.GetButton("Jump"))
{
aa = transform.position;
if (aa.y <= 1)
{
bb = transform.position;
isJumping = true;
}
}
Or then with coroutine:
void Update()
{
if (Input.GetButton("Jump"))
{
aa = transform.position;
if (aa.y <= 1)
{
bb = transform.position;
StartCoroutine(Jump());
}
}
}
IEnumerator Jump()
{
while (bb.y <= (aa.y + 4))
{
transform.Translate(0, (.25f * Time.deltaTime), 0);
bb = transform.position;
yield return new WaitForEndOfFrame();
}
}