- Home /
Problems with jump script
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;
}
}
}
Sorry should have added that these are declared in the beginning and i have this script attached to my main character.
static Vector3 aa;
static Vector3 bb;
Answer by Kossuranta · Mar 17, 2017 at 06:56 AM
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
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();
}
}
Thank you very much, that was extremely helpful :) going back to work on a game i had almost completed in unity 3... lol kinda forgot a lot and things changed ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220612113338im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
transform.Translate and transform.eulerAngles bug 0 Answers
Gradually scale platform? 4 Answers
Tranform.translate does not have constant speed 1 Answer
transform.Translate tutorial question 2 Answers