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 /
  • Help Room /
avatar image
1
Question by xJames720x · Oct 29, 2012 at 11:08 PM · jumpcharacter controller

Help with a simple jump script

I am writing a basic player controller and I am running into problems with my jump script. When I press spacebar, nothing happens.

I have this code in my update:

 //jump if player presses space bar
         if(Input.GetKeyDown(KeyCode.Space)){
                 Jump();
         }

And this is the actual jump method:

 //performs a jump 
     public void Jump(){
         Vector3 up = transform.TransformDirection(Vector3.up);
         rigidbody.AddForce(up*jumpForce);
     }

What am I doing wrong here? Thanks!

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

1 Reply

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

Answer by aldonaletto · Oct 29, 2012 at 11:23 PM

You're probably using a small jumpForce: AddForce in the default mode applies the force during a single physics cycle, and the result is an increase in velocity in the force direction proportional to the time the force is applied. You could use ForceMode.Impulse, for instance:

   rigidbody.AddForce(up * jumpForce, ForceMode.Impulse);

The Impulse mode concentrate in a single physics cycle all the acceleration the body would experience if the force were applied during one second. Another easier alternative is to directly add the desired velocity to the rigidbody:

  rigidbody.velocity += up * jumpSpeed; // jumpSpeed could be 8.0f, for instance

In any case, notice that you're transforming the up direction to the rigidbody space - it will jump to its local up direction, what may seem weird when the character isn't upright. If this is a problem, just use Vector3.up instead of up.

NOTE: The whole thing only makes sense if your character is a rigidbody. If it's a CharacterController, forget about forces and use the example script supplied by the docs in Move.

Comment
Add comment · Show 1 · 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 SimantoR · Feb 16, 2016 at 01:03 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
     //Player $$anonymous$$ovment Script
 public class Controls : $$anonymous$$onoBehaviour {
     public float Speed = 5.0f;
 
 
     //Updates Every frame
     void Update () {
         //Forward $$anonymous$$ovement
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))    {
             transform.Translate(Vector3.forward * Speed * Time.deltaTime);
         }
         //Backward $$anonymous$$ovement
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))    {
             transform.Translate(Vector3.back * (Speed - 2) * Time.deltaTime);
         }
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))    {
             transform.Translate(Vector3.left * Speed * Time.deltaTime);
         }
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
             transform.Translate(Vector3.right * Speed * Time.deltaTime);
         }
         if (Input.Get$$anonymous$$eyDown ("space")) {
             Vector3 up = transform.TransformDirection (Vector3.up);
             rigidbody.AddForce (up * 5, Force$$anonymous$$ode.Impulse);
         }
     }
 }

It shows: 'UnityEngine.Component' does not contain a definition for 'AddForce' and no extension method 'AddForce' accepting a first argument of type 'UnityEngine.Component' could be found

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

11 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

Related Questions

Why won't my character jump smoothly? 0 Answers

Character landing 0 Answers

how to jump in a 2D game 2 Answers

Jump logic issues 0 Answers

Rigidbody2D.velocity Interupts Rigidbody2D.AddForce in 2D Platformer 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