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 Le Brik · Feb 24, 2014 at 03:02 AM · javascriptphysicscharactercontroller

Problem with jumping, total noob.

Hello!

I am trying to make a simple script for a 2D game that controls the player's motion. It was working great until I added a walking animation; now the player simply teleports upwards a few inches and floats back down (obviously not what is intended). I am not sure what the problem is caused by.

Please feel free to tear apart the script in the process of helping me fix the issue. Let me know everything that I could do better, even if it is totally unrelated. Thanks!

 #pragma strict
 
 
 private var move :float;
 private var sprinting : float;
 var jumping : boolean;
 var jumpforce = Vector2 (0, 30000);
     
 var velo : Vector2;
     
 var animator : Animator;
 
     
 function Start () {
     animator = GetComponent("Animator");
 }
 
 function FixedUpdate () {
 
     
     move = 0.0;
     sprinting = 1.0;
 
     if (rigidbody2D.velocity.y <= 0.2){
         jumping = false;
     }
     else{
         jumping = true;
     }
 
     if (Input.GetKey (KeyCode.A) ==  true){
         move = -1;
         animator.SetBool("Walking", true);
     }
     
     if (Input.GetKey (KeyCode.D) == true){
         move = 1;
         animator.SetBool("Walking", true);
     }
 
     if (Input.GetKeyUp (KeyCode.A) ==  true){
         move = 0;
         animator.SetBool("Walking", false);
     }
     
     if (Input.GetKeyUp (KeyCode.D) == true){
         move = 0;
         animator.SetBool("Walking", false);
     }    
         
         
     if (Input.GetKey (KeyCode.LeftShift)){
         sprinting = 2;
     }
     
     if (Input.GetKeyUp (KeyCode.LeftShift)){
         sprinting = 1;
     }        
         
         
     if ((Input.GetKeyDown(KeyCode.W))){
         rigidbody2D.AddForce(Vector2(0,30000));
     }
         
     
     
     
 
     velo = rigidbody2D.velocity;
     velo.x = (4 * move * sprinting);
     rigidbody2D.velocity = velo;
 }
 
Comment
Add comment · Show 5
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 Benproductions1 · Feb 24, 2014 at 11:25 AM 0
Share

Please insert a space between your )'s and your {'s. We (as a community) may not be able to agree on the placement of the notorious {, but we can all agree on the fact that:

 if (condition){

Is very ugly and inconsistent. If you're going to put the { on the same line, at least separate it from the statement:

 if (condition) {

Also, you should have consistent spacing for function calls. Why define functions like this:

 function Name () {

But then use them like this:

 Name();

And then later use them like this:

 Name ();

Noone's going to tell you off for using either method, but at least stay consistent throughout your code.

That said, are you sure the position isn't being set by the animation itself?

avatar image Le Brik · Feb 24, 2014 at 02:03 PM 0
Share

Thanks, I'll try to be consistent from now on. How should I check to see if the animation is setting the position? By the way, walking back and forth works fine, just the jumping is messed up. Thanks for the help!

avatar image poncho · Feb 24, 2014 at 02:29 PM 0
Share

my best recommendation would be watch the tutorials of walkerboystudios, they are amazing, I learnt from them

avatar image Le Brik · Feb 24, 2014 at 02:37 PM 0
Share

But why doesn't this code work?

avatar image Flint Silver · Feb 24, 2014 at 03:18 PM 0
Share

with:

rigidbody2D.AddForce ( Vector2(0, > 200) );

you need to add:

rigidbody2D.AddForce( new Vector2(0, 200) );

or create a new variable Vector2 before and do like this:

Vector2 jumpForce = new Vector2(0, 200);

if (something happen)

  {

  rigidbody2D.AddForce(jumpForce);

  }


sorry for bad code...

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

driving game power ups 1 Answer

how to capsule cast from within Entities.ForEach job? ECS 1 Answer

I am having a problem trying to simulate football(soccer) physics 2 Answers

CharacterController unexpected collisions with MeshCollider 0 Answers

3rd person character controller not responding correctly 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