- Home /
my jump every 3 seconds program is not working
my jump every 3 seconds program is not working, I am new to unity and I suspect I have done something wrong and have come here to ask. the code behind it is in csharp. I am attempting to make a sphere with rigidbody jump but it just continues counting down past the point it was meant to jump and dosent reset either.
using UnityEngine; using System.Collections;
public class bounceeach3seconds : MonoBehaviour {
public float jumpheight = 500;
public float timer;
// Use this for initialization
void Start ()
{
timer = 3;
}
// Update is called once per frame
void Update ()
{
timer -= Time.deltaTime;
if (timer <= 0){
rigidbody.AddForce(0, jumpheight, 0);
timer = 3;
}
}
}
Ok it might not make a difference, if it's throwing no errors, but try this anyway.
You are comparing your floats to ints. In my past experience this has been ok but I'm not sure about C#. When using a value to be a float, place a decimal and f suffix.
timer = 3.0f;
Humour me and start there.
No that code is fine. The issue has to be somewhere else. I just put a cube in a scene and added your script to the cube with a rigidbody added to it and it's fine with that.
Yeah, I was really clutching at straws with that one :P
Almost 17k karma, I think we can let you off this one, you want to see me trying my hand at JS! Just painful.
Answer by Chris_Dlala · Jan 29, 2015 at 08:34 PM
Hi, I can't see anything immediately wrong. Do you receive any errors in the console window? The call to rigidbody
looks on the same gameobject as this script, is there a rigidbody on the same gameobject? All I can think of is that the rigidbody is null and therefore throwing an error, causing the update to end, never getting the the line after where you set the timer to 3.
Try adding a RigidBody reference as a member variable and assigning it in the editor. Or, add Debug.Log call at certain points to see how far/what lines get called.
I hope that helps =D
Answer by Shudrum · Jan 29, 2015 at 08:35 PM
Hi !
I have just copy/past your script and it works perfectly, only if the object with the rigidbody was on ground, else the falling velocity is too fast to work.
As your are new in Unity, sorry but stupid questions :
1 : Have you tried with a plane below your object ?
2 : For the (1) : do your object have a collider ?
3 : Did your script correctly attached to your object ?
And whilst we're on the subject of rigid-bodies what's the mass set to, if more than around 10 you'll probably not even notice it jump. The mass on my test cube was just set to 1.