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 Samael_00001 · Jun 03, 2015 at 01:02 PM · addforce

AddForce problem

Hi there! I'm using AddForce to make a character jump. But when I'm hitting Space very often - he flies like a rocket. How to fix that? Thank you!

Video

 void Update () 
     {
         horizontal = Input.GetAxis("Horizontal");
 
         if (Input.GetKeyDown (KeyCode.Space) && can_jump == 1) 
         {
             if (horizontal == 0)
             {    
                 animator.SetInteger("jump", 1);
                 gameObject.GetComponent<Rigidbody>().AddForce(0,200,0);
             }
             else
             {
                 if (horizontal > 0)
                 {
                    animator.SetInteger("jump_ahead", 1);
                 
                    gameObject.GetComponent<Rigidbody>().AddForce(0,200,100);
                 }
                 if (horizontal < 0)
                 {
                     animator.SetInteger("jump_ahead", 1);
 
                     gameObject.GetComponent<Rigidbody>().AddForce(0,200,-100);
 
                 }
             }
 
         }
 
         if (Input.GetKeyUp (KeyCode.Space)) 
         {
             animator.SetInteger("jump", 0);
             animator.SetInteger("jump_ahead", 0);
         }
     }


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 Bonfire-Boy · Jun 03, 2015 at 01:25 PM 0
Share

What behaviour are you ai$$anonymous$$g for?

If for example, you want a naturalistic system where the character can only jump when it's standing on something it can push against, you could check for that before applying the force.

Alternatively, if you want to be able to increase the jump height while in the air by hitting space again (but less dramatically), you could try zeroing the rigidbody's velocity (or just its upward component) before applying the force.

avatar image Samael_00001 · Jun 03, 2015 at 01:29 PM 0
Share

I want to know why does a character speeds up more then before and how to fix that.

avatar image Bonfire-Boy · Jun 03, 2015 at 02:06 PM 0
Share

He speeds up because you keep adding a force. You fix it by not doing that.

Beyond that.... it depends on the answer to my question.

avatar image Wolfdog · Jun 03, 2015 at 02:51 PM 0
Share

By watching your video, i don't see a problem. It does everything you asked it to do.

avatar image Samael_00001 · Jun 03, 2015 at 02:55 PM 0
Share

It moves faster and faster..

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HarshadK · Jun 03, 2015 at 01:21 PM

Allow your player to only jump if he is grounded.

Set a isGrounded boolean that you check for being true before adding jump force to the player. Once the player jumps set this boolean to be false and again set it to true when player lands on the ground to allow player to jump again. You can check the collision of player with ground to set this boolean to be true.

Comment
Add comment · 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 Samael_00001 · Jun 03, 2015 at 01:25 PM 0
Share

I'm already doing this.

 using UnityEngine;
 using System.Collections;
 
 
 
 public class PlayerController : $$anonymous$$onoBehaviour {
     float horizontal = 0;
     Animator animator;
     int can_jump = 0;
 
     // Use this for initialization
     void Start () 
     {
         animator = gameObject.GetComponent<Animator>();
         //print(gameObject.transform.rotation);
     }
 
 
 
     void OnCollisionEnter(Collision collision) 
     {
         if (collision.transform.tag == "ground") 
         {
             can_jump = 1;
         }
 
     }
 
     void OnCollisionExit(Collision collision)
     {
         if (collision.transform.tag == "ground") 
         {
             can_jump = 0;
         }
     }
 
     // Update is called once per frame
     void Update () 
     {
         horizontal = Input.GetAxis("Horizontal");
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) && can_jump == 1) 
         {
             if (horizontal == 0)
             {    
                 animator.SetInteger("jump", 1);
                 gameObject.GetComponent<Rigidbody>().AddForce(0,200,0);
             }
             else
             {
                 if (horizontal > 0)
                 {
                    animator.SetInteger("jump_ahead", 1);
                 
                    gameObject.GetComponent<Rigidbody>().AddForce(0,200,100);
                 }
                 if (horizontal < 0)
                 {
                     animator.SetInteger("jump_ahead", 1);
 
                     gameObject.GetComponent<Rigidbody>().AddForce(0,200,-100);
 
                 }
             }
 
         }
 
         if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Space)) 
         {
             animator.SetInteger("jump", 0);
             animator.SetInteger("jump_ahead", 0);
         }
     }
     
     Vector3 temp1;
     Quaternion temp2;
 
     void FixedUpdate()
     {
         animator.SetFloat("walking_f", horizontal);
         animator.SetFloat("walking_b", horizontal);
 
         if (horizontal >= 0.1)
         {
             temp2 = transform.rotation;
             temp2.y = 0;
             transform.rotation = temp2;
 
             temp1 = transform.position;
             temp1.z += 3.5f* Time.deltaTime; 
             transform.position = temp1;
         }
 
         if (horizontal < 0)
         {
 
             temp2 = transform.rotation;
             temp2.y = 180;
             transform.rotation = temp2;
 
             temp1 = transform.position;
             temp1.z -= 3.5f* Time.deltaTime; 
             transform.position = temp1;
         }
     }
 }
 

 
avatar image HarshadK · Jun 03, 2015 at 01:48 PM 0
Share

Rather than setting the can_jump from OnCollisionExit try to set it once the player makes the jump. Also it is better to use a boolean for can_jump rather than an int.

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

21 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

Related Questions

physics based path system? 1 Answer

Help with AddForce + dragging objects 1 Answer

Check for rigidbody presence when in collision with a trigger 2 Answers

The direction of travel is not rotating with the projectile in the spinning GameObject. 5 Answers

Stopping a rigidbody at target 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