- Home /
Jumping through the roof!
Hello,
I've made the follwing jump code. Time is frozen and the character is stuck mid air. When you click on the screen force is applied so he jumps for a bit.
But once every 10-15 times, he goes through the roof when i first click the screen. I have no idea why this is happening or how to fix it. i've tried a lot of different things but since i have no idea what the problem is i cannot fix it. Help would be appreciated.
You can see for yourselves in a version i made for kongregate ( press space to jump)
http://www.kongregate.com/games/axell/bouncy-ninja
using UnityEngine;
using System.Collections;
[System.Serializable]
public class JumpNinja : MonoBehaviour {
// The fx,fy,fz - forces in x/y/z direction. by default, fz = 0
public float fx;
public float fy;
public float fz;
public GUIText tapText;
void Awake(){
Time.timeScale=0;
Debug.Log("timescale");
//rigidbody.AddForce(fx,fy,0);
}
void Start () {
rigidbody.AddForce(fx,fy,0);
}
void Update(){
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Working");
if (Time.timeScale == 0)
{
tapText.guiText.enabled = false;
Time.timeScale=1;
}
audio.Play();
rigidbody.AddForce(0,fy,0);
rigidbody.velocity = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);
//rigidbody.AddForce(0,fy,0);
}
}
}
@Dimwood I did try fixedupdate. It still happens :( @Rockyourteeth Thanks! The android version is even better
Bump for this? It's really critical and i have no idea why it happens.
Answer by rockyourteeth · Mar 07, 2014 at 09:32 PM
Just a guess, but could it be that you're using "AddForce" both in the Start and the Update functions? Might this cause the character to get double the force on its first step?
Sorry, I played the game over and over to try to find a repeatable pattern, but it does seem to happen at random.
EDIT: This wasn't the problem (see comments below). The problem turned out to be that:
There's some weird behavior when time.timescale is set in the Awake() or Start() functions. Try putting it in the Update() function, and make it so it only runs the first time.
I've tried abunch of different solutions, but to no avail. But i did discover something interesting. I can reproduce it 100% now if i do it like this
if (Time.timeScale == 0) {
tapText.guiText.enabled = false; Time.timeScale=1; rigidbody.AddForce(fx,fy,0); }
ins$$anonymous$$d of putting it in the start. Now that is strange.
According to this thread, it looks like there's some weird behavior when time.timescale is set in the Awake() or Start() functions. Try putting it in the Update() function, and make it so it only runs the first time.
Thanks for the answer. Putting the TimeScale in FIxedUpdate did the trick.
Great! Glad I could help! I added that to my answer, so please label that as the correct answer if it indeed fixed it.
Your answer
Follow this Question
Related Questions
Adding double jump to physics platformer kit? 2 Answers
Interpolate problem? Jump isn't smooth... 1 Answer
Jump animation won't move forwards 1 Answer
How can I make a physics object jump a given height on collision regardless of current velocity? 1 Answer
Set Velocity at relative position. 2 Answers