• 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
Question by Wakeful · May 19, 2014 at 06:08 PM · c#unity 2djumpscript errorground

2D C# Jump Script Addforce

Hey, im new to unity and scripting in C# and I need a bit of advice on scripting. I am trying to make my player jump when he hits W and is touching the ground. I Have added a rigidbody2d and and I have created a simple ground checker and when I use

Transform.translate

(Not the exact script just off memory) but it is very choppy, so I am trying to use this

 rigidbody2D.AddForce(Vector2.up * jumpSpeed, ForceMode.Acceleration);

 

but its not working. It gives me an error: Addforce takes two arguments Sorry if I'm unclear thanks for your time. If you need more information of you don't understand what I'm saying just say something and I'll try to fix it.

Full script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
     public float moveSpeed = 10f;
     public float jumpSpeed = 10f;
 
     void Update()
     {
         //Next two if statements are for moving left and right
                 if (Input.GetKey (KeyCode.D))
                         transform.Translate (new Vector2 (1, 0) * moveSpeed * Time.deltaTime);
 
                 if (Input.GetKey (KeyCode.A))
                         transform.Translate (new Vector2 (-1, 0) * moveSpeed * Time.deltaTime);
         }
 
     //Used for checking if the player is touching the ground 
         void OnCollisionStay2D(Collision2D coll ) // C#, type first, name in second
         {
         if (coll.gameObject.tag == "Ground" &&(Input.GetKey(KeyCode.W)))
         {
             //Jump Script
             rigidbody2D.AddForce(Vector2.up * jumpSpeed, ForceMode.Acceleration);
 
         }
             
 
 
 
     
     }
 
 }


Comment
eveyava

People who like this

1 Show 0
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
Best Answer

Answer by serenefox · May 19, 2014 at 09:08 PM

I remember reading somewhere on either Unity Answers or the Unity Forums that "ForceMode" does not work with rigidbody2D.Addforce, you have to take that out I am pretty sure.

Just did a quick search and this came up as the first hit, here is the link, hope this can get you going in to right direction: http://answers.unity3d.com/questions/574864/rigidbody2d-and-forcemodeaddvelocity.html

Comment
eveyava

People who like this

1 Show 2 · 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 Wakeful · May 20, 2014 at 12:06 AM 0
Share

Ya, I figured it out and this seems to work.

rigidbody2D.AddForce(Vector3.up * jumpSpeed * Time.deltaTime);

Thanks again for your time.

avatar image KiwiKevKev · Mar 25, 2017 at 10:44 AM 0
Share

I try a little and figured out (for Unity 5.5.2f1) that you can replace "ForceMode.Acceleration" with "ForceMode2D.Force. It also works.

 rigidbody2D.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Force);
avatar image

Answer by gaurav94 · Dec 16, 2017 at 06:30 PM

its solve in problem .............................................................................. transform.GetComponent().AddForce(Vector3.up*jumpSpeed,ForceMode.Acceleration);

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Jump/grounding bugs, advice appreciated 1 Answer

RayCast to test if player is grounded 1 Answer

My script only partially works 2 Answers

Making a character jump with the Character Controller Component instead of Capsule Collider 0 Answers

Making player jump when left mouse clicked 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