Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nexus Blues · Feb 28, 2013 at 03:02 AM · 2djavascriptrigidbodyjumpingplatformer

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tarlius · Feb 28, 2013 at 06:48 AM 0
Share

If its not quite what you want, you should be able to at least use it as a base.

avatar image Nexus Blues · Feb 28, 2013 at 06:49 AM 0
Share

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.

avatar image fafase · Feb 28, 2013 at 06:51 AM 0
Share

You can modify the size of the capsule to make it "almost" like a cube, more like a rounded corner cube...

avatar image Nexus Blues · Feb 28, 2013 at 06:52 AM 0
Share

I'll give it a shot

avatar image Tarlius · Feb 28, 2013 at 07:03 AM 0
Share

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?

Show more comments
avatar image
0

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nexus Blues · Feb 28, 2013 at 06:00 AM 0
Share

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

avatar image liamcary · Feb 28, 2013 at 06:22 AM 0
Share

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;
    }
 }
avatar image Nexus Blues · Feb 28, 2013 at 06:44 AM 0
Share

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'.

avatar image liamcary · Feb 28, 2013 at 07:01 AM 0
Share

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.

avatar image Nexus Blues · Feb 28, 2013 at 07:03 AM 0
Share

Okay so ignore the void update? it's supposed to be function update?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges