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 Makiavel · Feb 25, 2014 at 02:38 PM · charactercontrollerjumpjumping

Problems with jumping script

I have tried to create an alternative to the jumping section of the Character Motor to allow the player, no matter where I turn the gravity, to jump in opposite direction. But I encounter a problem with the basic element of jumping, the character sometimes refuses to jump, instead wriggling a little on the ground. Sometimes he shakes, and then jumps. I am not sure what causes this, as I have tried to use the most basic function.

I have reduced the code to the very essence of what should make the character jump, so the problem is definitely somewhere here. Does anybody know what stupid mistake I made this time?

   using UnityEngine;
     using System.Collections;
     
     public class Player_Jump_Script_New : MonoBehaviour 
     {
     
         public Vector3 moveDirection;
         public CharacterController Controller;
         public float gravity;
         
         void Start () 
         {Controller = GetComponent<CharacterController>();}
         
         void Update () 
         {
         if(CanJump==true)
         {
            if (Input.GetButtonDown("Jump"))
            {
            CanJump=false;
            moveDirection.y = jumpSpeed;
            StartCoroutine(JumpOver(0.5f));
            }
         Controller.Move(moveDirection * Time.deltaTime);
         }
         }
 
 
 
 
 
     IEnumerator JumpOver (float temp)
     {
         yield return new WaitForSeconds (temp);
         if(IsJumping==true)
         {
         moveDirection.y = -gravity;
         CanJump=true;
         }
     }    
     
     }
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 Makiavel · Feb 26, 2014 at 08:37 AM

Forger it, I have created a normal rigidbody character movement system, so this isn't a problem anymore)

if anybody will find this answer searching to solve a similar problem, here's my solution:

 using UnityEngine;
 using System.Collections;
 
 public class RigidbodyJump : MonoBehaviour {
     
     
     
     
     public bool CanJump;
     public float Player_Gravity;
     public float MinJumpHeight;
     public float force;
     public bool IsJumping;
     public bool CheckIfCanJump;
     
     public float speed;
     public float WalkingSpeed;
     public float RunningSpeed;
     
     public float RunningTimer;
     public float RunningTimerMax;
     public float RunningTimerDrain;
     public float RunningTimerRecharge;
     private bool IsRunning;
 
     public Vector3 MovementDirection;
 
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void FixedUpdate () 
     {
             if(Input.GetKey("left shift")&&RunningTimer>0)
         {
             IsRunning=true;
         }
         else
         {
             IsRunning=false;
            }
 
         
         if(IsRunning==true)
         {
             speed = RunningSpeed;
             RunningTimer -= RunningTimerDrain;
         }
         else
         {
             speed = WalkingSpeed;
             
             if(RunningTimer<=RunningTimerMax)
             {
             RunningTimer += RunningTimerRecharge;
             }
             
         }
 
         if(Input.GetButtonDown("Jump"))
         {    
                     if(CanJump==true)
                     {
                     transform.rigidbody.AddForce(Vector3.up * force);
                     IsJumping=true;
                     CanJump=false;
                     StartCoroutine(CheckIfCanJumpAgain(0.1f));    
                     }
                     
         }
                 
                 if(CheckIfCanJump==true)
                 {
                     RaycastHit hit;
                        if (Physics.Raycast(transform.position, -transform.up, out hit, 100.0F))
                     {
                     
                     if(hit.distance<=MinJumpHeight)
                     {
                     CanJump=true;
                     IsJumping=false;
                     CheckIfCanJump=false;
                     }
                     }
                 }
     
                 
         
         if(Input.GetButton("w"))
         {
             MovementDirection.z = 1;
         }
         else
         {
             MovementDirection.z = 0;
         }
         
         if(Input.GetButton("s"))
         {
             MovementDirection.z = -1;
         }
         
         
         if(Input.GetButton("a"))
         {
             MovementDirection.x = -1;
         }
         else
         {
             MovementDirection.x = 0;
         }
         
         if(Input.GetButton("d"))
         {
             MovementDirection.x = 1;
         }
         
         transform.rigidbody.AddForce(-Vector3.up * Player_Gravity);
         
         transform.Translate(MovementDirection * speed);
 
     }
     
 
     IEnumerator CheckIfCanJumpAgain (float temp)
     {
         yield return new WaitForSeconds (temp);
         CheckIfCanJump = true;
     }
     
     public void SwitchGravity()
     {
         Player_Gravity = -Player_Gravity;
     }
 
     
 }
Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Jumping script WONT WORK!!!!!!!! for 2D 1 Answer

Need help with making a character jump. 1 Answer

How do you make a characterController jump automatically when it reaches the edge of a platform? 0 Answers

jump with character controller best way? 1 Answer

How do I fix my player jumping code? 2 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