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 superluigi · Jun 03, 2013 at 10:09 PM · javascriptjumpnoob

Help with my double jump script

I am trying to get my character to jump in mid air either left right or just straight up depending on which key I am pressing and also need it to ignore my current velocity however I have not been able to achieve this. I have tried everything from making a doublejump function with else if statements to consulting ancient gypsy fortune tellers but have not been able to get my desired result. Heres my script:

 var speed : float = 4.0;
 var jump : float = 10.0;
 var dJump : float = 10.0;
 var gravity : float = 20.0;
 var dJumpV : float = -4.0;
 var dJumpV0 : float = 4.0;
 
 private var canDJump : boolean = false;
 private var velocity : Vector3 = Vector3.zero;
 
 var controller : CharacterController;
 
 function Update() 
 {
     
     if (!controller.isGrounded && canDJump && Input.GetKey("a") 

`|| !controller.isGrounded && canDJump && Input.GetKey("left")) { ldJump(); } if (!controller.isGrounded && canDJump && Input.GetKey("d") || !controller.isGrounded && canDJump && Input.GetKey("right")) { rdJump(); }

     if (!controller.isGrounded && canDJump && Input.GetButtonDown("Jump"))
     {
         velocity.y = dJump;
         canDJump = false;
     }
     
     if (controller.isGrounded)
     
     {
         velocity = Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0);
         velocity = transform.TransformDirection (velocity);
         velocity *= speed;
             
         if (Input.GetButtonDown ("Jump"))
             {
                 velocity.y = jump;
                 canDJump = true;
             }
     }
     
     velocity.y -= gravity * Time.deltaTime;
     controller.Move (velocity * Time.deltaTime);
 }    
 
 function ldJump()
 {
     if (Input.GetButtonDown ("Jump"))
             {
                 velocity.y = jump;
                 velocity.x = dJumpV;
             }
 }
 
 function rdJump()
 {
     if (Input.GetButtonDown ("Jump"))
             {
                 velocity.y = jump;
                 velocity.x = dJumpV0;
             }
 }
Comment
Add comment · Show 3
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 darthbator · Jun 03, 2013 at 10:32 PM 0
Share

your code paste is all jacked. You'll need to clean that up before anyone looks at this.

avatar image superluigi · Jun 03, 2013 at 11:28 PM 0
Share

So I figured it out sorta except that I can't jump straight up if my first jump was to either side hopefully someone can help me with that part. Anyway there's the code that works for me if anybody needs it.

avatar image superluigi · Jun 03, 2013 at 11:45 PM 0
Share

it can't get any cleaner than that at least not from me.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mr-JWolf809 · Jun 04, 2013 at 12:14 AM

Heya,

I think you have complicated the double jump a bit too much. You can use the same approachas you used in your normal jump to figure out the Horizontal movement.

Try this:

#pragma strict

var speed : float = 4.0; var jump : float = 10.0; var dJump : float = 10.0; var gravity : float = 20.0; var dJumpV : float = 4.0; private var canDJump : boolean = false; private var velocity : Vector3 = Vector3.zero; var controller : CharacterController;

function Update() {

 if (!controller.isGrounded && canDJump && Input.GetButtonDown ("Jump"))
 {
     velocity = GetHorizontalMovementDirection();
     velocity *= dJumpV;
     velocity.y = dJump;
     canDJump = false;
 }
 
 if (controller.isGrounded)
 {
     velocity = GetHorizontalMovementDirection();
     velocity *= speed;
     if (Input.GetButtonDown ("Jump"))
     {
         velocity.y = jump;
         canDJump = true;
     }
 }
 velocity.y -= gravity * Time.deltaTime;
 controller.Move (velocity * Time.deltaTime);

}

function GetHorizontalMovementDirection() { var direction : Vector3 = Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0); return transform.TransformDirection (direction); }

Comment
Add comment · Show 5 · 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 superluigi · Jun 04, 2013 at 12:42 AM 0
Share

Wow worked like a charm thank you. Time for me to look over your code and figure out why it works. I guess what has me stuck is line 19 in your script. why is velocity *= dJumpV only affecting the X axis?

avatar image TheRealOsme · Feb 23, 2014 at 01:24 AM 0
Share

How would you make this so you can move left or right in mid air?

avatar image superluigi · Feb 23, 2014 at 04:54 AM 0
Share

@TheRealOsme wow you're lucky I suddenly decided to look at my old questions activity. The reason you can't move in the air is because the controller needs to be grounded in order to be able to move horizontally. If you eraser line 24 along with 25 and 33 which have the {}that belong to that if statement, you should be able to move while jumping. Haha when I first saw this code it was so confusing, now it looks so simple.

avatar image TheRealOsme · Feb 25, 2014 at 08:46 PM 0
Share

when I remove lines 24 along with 25 and 33 i get the error "UnassignedRefrenceException: The variable controller of "CharacterController' has no been assigned"

avatar image superluigi · Feb 26, 2014 at 01:14 AM 0
Share

I'm on the wiiU right now, I'll get on the pc later and help you get the code working. However, it sounds like you haven't assigned the character controller to the controller variable in the inspector. You may have reset the script or assigned the script to a new object. $$anonymous$$ake sure you drag the character controller into the controller variable. If this confuses you let me know, I'll gladly answer any questions you might have

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Translation of an object 2 Answers

AddForce to sphere 1 Answer


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