- Home /
I am having issues with my Jumping Script.
(I am using a Rigidbody and it's a 2D platformer) Sorry if this is a stupid question but I'm new to Unity and coding in general. When i add a Timer or OnCollisionEnter to my jumping script it acts as though i am hitting a object above me and stops me from jumping. When i remove the Timer/OnCollisionEnter the character can jump but can jump infinitely. Any help would be greatly appreciated.
(JavaScript)
function Start () {
}
var moveSpeed : float = 0.15;
var jumpSpeed : float = 0.75;
function Update(){
if (Input.GetKey("d"))
transform.Translate(-moveSpeed,0,0);
if (Input.GetKey("a"))
transform.Translate(moveSpeed,0,0);
if (Input.GetButtonDown("Jump"))
transform.Translate(0,jumpSpeed,0);
}
What can i do to make sure i cannot jump in the air?
Answer by fafase · Feb 28, 2013 at 06:36 AM
Using the character controller would be much more simple. You have the isGrounded variable that tells you if you are grounded, it prevents going through object, it reports which part of the capsule is colliding (side, below, above).
All in all, you should give it a try.
http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.html
If its not quite what you want, you should be able to at least use it as a base.
Hey, i have tried it but my object isn't a capsule and i don't know how to make it a cube so it didn't really work.
You can modify the size of the capsule to make it "almost" like a cube, more like a rounded corner cube...
Can you not just swap the capsule for a box collider?
I've not done much in true 3D, but they both use the same base class. Is there functionality specific to capsule collider the controller relies on?
Answer by liamcary · Feb 28, 2013 at 05:33 AM
I'm not sure what you're talking about with the timer and OnCollisionEnter seeing as you haven't posted that functionality, but that jump shouldn't work properly. With "GetButtonDown()" the transform will only translate for one frame. So it's going to move up 0.75 units and then fall back down (if you're using gravity). If you want the transform to keep translating upwards you should use Input.GetButton(), it is the equivalent of Input.GetKey.
And on an unrelated note, if you're manually translating anything you should use something like:
if(Input.GetKey("d"))
transform.Translate(-moveSpeed * Time.deltaTime, 0, 0);
You'll have to scale the value of "moveSpeed" up because deltaTime is generally a tiny number, but it will be framerate independent.
Hey, thanks for the advice and i forget to change the Input.GetButtonDown, i normally used Input.Get$$anonymous$$ey i was just experimenting.
OnCollisionEnter script:
var collisionObject : GameObject;
function OnCollisionEnter(collision : Collision){
if (collision.gameObject.name == collisionObject){
if(Input.Get$$anonymous$$ey("space"))
transform.Translate(0,jumpSpeed,0);
}
}
Timer script:
var jumpInterval: float = 1;
private var nextJump: float = 0;
if (Input.Get$$anonymous$$ey("space") && Time.time > nextJump){
nextJump = Time.time + jumpInterval;
transform.Translate(0,jumpSpeed,0);
}
When i use the Input.Get$$anonymous$$ey my character can essentially fly. How do i prevent midair jumping? Sorry for any inconvenience and thank you for all your help.
-Aren
Is the collisionObject in your OnCollisionEnter script the ground? So that the player only jumps once they're back on the ground? At the moment it looks like your player can jump once every 1 second, according to the timer script.
In the OnCollisionEnter function you compare the "collisionObject" GameObject with the NA$$anonymous$$E of the object that triggered the OnCollisionEnter function. You should replace:
if(collision.gameObject.name == collisionObject)
with
if(collision.gameObject == collisionObject)
But then both the OnCollisionEnter and Timer scripts will be able to make the player jump. You should have the player jump only when both conditions are met. You could set a boolean value (isOnGround) to true in the OnCollisionEnter function and then do something along the lines of:
void Update()
{
if(Input.Get$$anonymous$$ey("space") && Time.time > nextJump && isOnGround == true)
{
transform.Translate(0, jumpSpeed, 0);
isOnGround = false;
nextJump = Time.time + jumpInterval;
}
}
Is this what i am supposed to have? By the way i'm sorry this is the first time i have tried to code something on my own X.X
function Start () { }
var moveSpeed : float = 0.15; var jumpSpeed : float = 0.75; var jumpInterval : float = 1;
private var nextJump : float = 0;
function Update(){
if (Input.Get$$anonymous$$ey("d")) transform.Translate(-moveSpeed,0,0);
if (Input.Get$$anonymous$$ey("a")) transform.Translate(moveSpeed,0,0);
function OnCollisionEnter(collision : Collision){ var isOnGround : boolean = true; } void Update() {
if (Input.Get$$anonymous$$ey("space")&& Time.time > nextJump && isOnGround == true) { transform.Translate(0,jumpSpeed,0); isOnGround = false; nextJump = Time.time + jumpInterval; } }
I'm getting an error saying "expecting (, found 'OnCollisionEnter'.
That error is because you're not closing the Update function, you're missing a "}". Also, that "void Update()" should be "function Update()". I accidentally wrote in C# earlier.
Okay so ignore the void update? it's supposed to be function update?
Your answer
Follow this Question
Related Questions
How would I make a route that shows where player moves beforehand? 0 Answers
Has anyone made a successful Pixel Perfect Camera script for platformer games? 2 Answers
2D Platformer Jump while Running Android 1 Answer
Different results between "ContactPoint" and "ContactPoint2D" 0 Answers
2D jumping raycast question 0 Answers