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 Jsim · Jun 18, 2014 at 06:18 AM · rigidbodydouble jump

Trying to make a doublejump script, problems using rigidbody

So I'm new to unity and I have been researching double jump for a while. It's apparently difficult... at least it is for me... So I have set up a custom Player Controller script, a rigid body, and a Character Controller (which I don't think I am even using anymore...). I am not brand new to JS but I am not exactly a seasoned veteran when it comes to actually using it as a scripting language outside of HTML... What I am trying to do is make a simple double jumping character by following other people's scripts and tutorials, but I am trying to do it my own way, a simpler way, and in general, a different way from which I have seen done. I thought it would be possible to do this with my current setup without having to forfeit what I have already done - however through my struggles I have redone basically everything from scratch anyways...

I digress...

Here is the code for my Script that is supposed to move the Character left and right and allow for double jump if the character is able. Can someone please tell me what I am doing wrong? This is mostly a learning experience. I don't learn well from Youtube tutorials, so If someone could help me understand what I am doing that would be great!

 var autoRotate : boolean = true;
 var maxRotationSpeed : float = 360;
 var airJumpMax : int = 1;
 var jumpForce : float = 8;
 var airJumpForce : float = jumpForce * 0.6;
 var walkSpeed : float = 8;
 
 private var xMove : int = 0;
 private var airJumpAmt : int = 0;
 private var isGrounded : boolean = false;
 
 function Awake () {
     motor = GetComponent(CharacterMotor);
 }
 
 function Update () {
     //Double jump raycasting
     if(Physics.Raycast(transform.position, -Vector3.up, 1)) {
         isGrounded = true;
         airJumpAmt = airJumpMax;
     }
     if(Input.GetKeyDown(KeyCode.Space)){
        if(isGrounded) {
            JumpNormal();
        }else if(airJumpAmt > 0) {
            airJumpAmt -= 1;
            JumpInAir();
        }
     }
     
     xMove = 0;
     xMove = Input.GetAxisRaw("Horizontal") * walkSpeed;
 }
 
 function FixedUpdate(){
     rigidbody.velocity = new Vector3(xMove*walkSpeed,0,0);
 }
 
 function projectOntoPlane (v : Vector3, normal : Vector3) {
     return v - Vector3.Project(v, normal);
 }
 
 function JumpNormal () { rigidbody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); }
  
 function JumpInAir () { rigidbody.AddForce(Vector3.up * airJumpForce, ForceMode.Impulse); }
 
 function constantSlerp (from : Vector3, to : Vector3, angle : float) {
     var value : float = Mathf.Min(1, angle / Vector3.Angle(from, to));
     return Vector3.Slerp(from, to, value);
 }
 
 // Require a character controller to be attached to the same game object
 @script AddComponentMenu ("Character/Platform Input Controller");



EDIT: So I had to change some original values a little bit but eventually yes my double jump now works. However I am still having problems moving horizontally. The character, currently, slowly drags to the ground because of this

function FixedUpdate(){ rigidbody.velocity = new Vector3(xMove*walkSpeed,0,0); }

I am not sure how to move my player horizontally, while keeping vertical and horizontal momentum, using my current setup... I do have a character controller in the player too, not just my script, but I haven't used it because... Well... I don't know what it does or how to use it. Basically this script causes the character to slowly, and choppily, sink to the ground from starting position. It cannot jump and cannot move. That is because of what is in the "FixedUpdate" function. I fixed the double jump but the character can't move left or right without totally spazzing out.

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 Andres-Fernandez · Jun 18, 2014 at 06:39 AM 0
Share

Why do you say you are doing wrong? What does your code do? What do you expect it to do? $$anonymous$$nowing that could help everyone in trying to help you.

It seems to me that you could have some trouble with the logic used to check if the character is grounded. Ins$$anonymous$$d of using a raycast for the double jump, why not use a state machine (just for the jump)? If state equals grounded call JumpNormal, else if state equals jumpingNormal call JumpInAir.

JumpNormal function sets state to jumpingNormal and going back to the ground sets it to grounded.

avatar image Jsim · Jun 18, 2014 at 07:05 PM 0
Share

Yea I do not actually know how to use a state machine or whatever you are talking about... And I'll edit the post with your suggestions thanks!

avatar image SethSR · Jun 19, 2014 at 07:35 AM 0
Share

You could try function FixedUpdate() { rigidbody.velocity.x = x$$anonymous$$ove * walkSpeed; } ins$$anonymous$$d of what you have, as right now you're setting vertical velocity to zero at the beginning of every physics update.

1 Reply

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

Answer by indeed005 · Jun 18, 2014 at 06:51 AM

Is your first jump propelling your character more than 1 unit above the ground?

I ask because you are raycasting 1 unit down. If not, then this clause will never be reached:

    }else if(airJumpAmt > 0) {
        airJumpAmt -= 1;
        JumpInAir();
    }

Your jumpforce is 8, but I don't know how heavy your rigidbody is, or if you are using normal gravity. Put a breakpoint on it and find out

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Any tips to fix and improve this movement script? 0 Answers

Double jump does not work why? 0 Answers

Instantiating two rigidbody at a time 0 Answers

Making 3D Objects Stick On Other Objects Like A Grid? 2 Answers

Why is my object not moving 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