Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Rembo4Fight · Dec 30, 2016 at 01:05 PM · 2dscripting problemjumpjumpingforce

Jump Force/Velocity is Wrong and Strange. Help

Hello, it's been a long time i was trying to resolve this problem by myself, but I don't see where it is. The problem is in my Jumping - it's very glitchy and my character jumps on different hight everytime. If you can help and need some information, please ask, i'll give every info i can :) I hold my space button to jump and this video will show what happening - https://www.youtube.com/watch?v=b26j5ISyivw It's just randomly jumping and I want in to be a Straight Jump Move like in every game I think the problem is in JumpForce, please help!alt text

Here is my Script PlayerController

using UnityEngine; using System.Collections;

public class PlayerControllerScript : MonoBehaviour {

 public float maxSpeed = 10f;
 public float jumpForce = 700f;
 bool facingRight = true;
 Animator anim;
 bool grounded = false;
 public Transform groundCheck;
 public float groundRadius = 0.2f;
 public LayerMask whatIsGround;
 public float move;
 public float score;
 public float spawnX, spawnY;


 void Start () 
 {
     anim = GetComponent<Animator>();

     spawnX = transform.position.x;
     spawnY = transform.position.y;
 }

 void Update()
 {
     if (grounded && Input.GetKey ("space")) {

         anim.SetBool ("Ground", false);

         GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0f, jumpForce));


         GetComponent<Rigidbody2D> ().velocity = (new Vector2 (move * maxSpeed * Time.deltaTime, GetComponent<Rigidbody2D> ().velocity.y));

     }

     if (Input.GetKeyDown (KeyCode.R))
         Application.LoadLevel (Application.loadedLevel);

 }

 void FixedUpdate() 
 {
     grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);

     float move = Input.GetAxis ("Horizontal");

     anim.SetBool ("Ground", grounded);

     anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);

     anim.SetFloat ("Speed", Mathf.Abs (move));

     GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

     if (move > 0 &&!facingRight)
         Flip ();
     else if (move < 0 && facingRight)
         Flip ();
 }
     
     
 void Flip()

     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
     

 void OnCollisionEnter2D(Collision2D col){
     if (col.gameObject.layer == LayerMask.NameToLayer ("killObjects") || col.gameObject.layer == LayerMask.NameToLayer ("dieCollider"))
         transform.position = new Vector3 (spawnX, spawnY, transform.position.z);

     if (col.gameObject.name == "endLevel")
         Application.LoadLevel ("scene2");
     }

 void OnTriggerEnter2D(Collider2D col) 
 {
     if (col.gameObject.layer == LayerMask.NameToLayer ("food")) {
         Destroy (col.gameObject);
         score++;
     }
 }

}

screen-shot-2016-12-30-at-155736.png (196.9 kB)
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
0
Best Answer

Answer by LucianoMacaDonati · Dec 30, 2016 at 04:18 PM

Hello !! I'm not completely sure where your problem is, but we could start narrowing it down by removing the bad practices, and some things I see could cause strange behaviors.

For jumping, I would recommend applying a force in the .up direction instead of manually modifying the velocity in your update (you're actually doing it in BOTH update and fixedUpdate). Look here to achieve this.

Besides this, I would strongly recommend that you don't use GetComponent in your updates. This is somewhat an expensive operation and it can be simply solved in several ways.

Let me know how it goes. Good luck.

Comment
Add comment · Show 16 · 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 Rembo4Fight · Dec 30, 2016 at 05:06 PM 0
Share

Okay,thanks for reply!) I changed my GetComponent to private function in script

 private Rigidbody2D rb2d;
 
     void Start () 
     {
         rb2d = gameObject.GetComponent<Rigidbody2D> ();
 
         anim = GetComponent<Animator>();
 
         spawnX = transform.position.x;
         spawnY = transform.position.y;
     }

But I'm not sure that I add .up correctly, but no errors are showed)

 void Update()
     {
         if (grounded && Input.Get$$anonymous$$ey ("space")) {
 
             anim.SetBool ("Ground", false);
 
             rb2d.AddForce (Vector2.up * jumpForce);
 
             rb2d.velocity = (new Vector2 (move * maxSpeed * Time.deltaTime, rb2d.velocity.y));
 
         }
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R))
             Application.LoadLevel (Application.loadedLevel);
 
     }
 
     void FixedUpdate() 
     {
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
 
         float move = Input.GetAxis ("Horizontal");
 
         anim.SetBool ("Ground", grounded);
 
         anim.SetFloat ("vSpeed", rb2d.velocity.y);
 
         anim.SetFloat ("Speed", $$anonymous$$athf.Abs (move));
 
         rb2d.velocity = new Vector2 (move * maxSpeed,rb2d.velocity.y);
 
         if (move > 0 &&!facingRight)
             Flip ();
         else if (move < 0 && facingRight)
             Flip ();
     }

Do you think I must to get rid of some functions for velocity or AddForce in Update or FixedUpdate ? And i am doing right what you said?)

avatar image LucianoMacaDonati Rembo4Fight · Dec 30, 2016 at 06:15 PM 0
Share

Why are you changing the velocity on both updates ? Why update the velocity at all ??

avatar image LucianoMacaDonati LucianoMacaDonati · Dec 30, 2016 at 08:03 PM 1
Share

Why don't you remove the "grounded" check for now until you get the jumping working ? Simply do

 if (Input.Get$$anonymous$$eyDown("space"))
        rb2d.AddForce (Vector2.up * jumpForce);

Comment out the other stuff for now.

Show more comments

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

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

Related Questions

Jumping on enemies help 0 Answers

problem with jump script 2 Answers

I can't do jump in my 2D game 1 Answer

Unity 2D Enemy jump Question (Frog jump) 1 Answer

Increase jump speed colliding on a box ?? 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