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 VladCastillo · Oct 27, 2020 at 09:18 AM · falling

Character don`t jump

Character dont jump

using UnityEngine;

 // MoveBehaviour inherits from GenericBehaviour. This class corresponds to basic walk and run behaviour, it is the default behaviour.
 public class Hannamove: MonoBehaviour
 {
     public float speednow = 1.0f;
     public float walkSpeed = 2.0f;                // Default sprint speed.
     public float runSpeed = 2.0f;                // Default sprint speed.
     public string jumpButton = "Jump";              // Default jump button.
     public float jumpnow = 10f;
     public float jumpforce = 10f;          // Default horizontal inertial force when jumping.
     public float gravityyo = 10f;
     private Vector3 dir = Vector3.zero;
 //private
     public bool jump;                              // Boolean to determine whether or not the player started a jump.
     public bool isColliding;                       // Boolean to determine if the player has collided with an obstacle.
     public Rigidbody playerrig;
 
     // Start is always called after any Awake functions.
     void Start(){
         
     }
 
     // Update is used to set features regardless the active behaviour.
     //////////////////////
    public void MoveInDirectionOfInput() {
        
   //CharacterController controller = GetComponent<CharacterController>();  
      
            dir.x = Input.GetAxis("Horizontal");
            dir.z = Input.GetAxis("Vertical");
            ///
 
    Vector3 camDirection = Camera.main.transform.rotation * dir;
    Vector3 targetDirection = new Vector3(camDirection.x, 0f, camDirection.z);
    
    if (dir != Vector3.zero) {
       transform.rotation = Quaternion.Slerp(
       transform.rotation,
       Quaternion.LookRotation(targetDirection),
       Time.deltaTime * speednow);
       ///
    }
    
 /////////////////////////
     
         if (!jump && Input.GetButtonDown(jumpButton))   {  
             jump = true;
             jumpnow = jumpforce;    
             targetDirection.y = jumpforce;                
         }
         
         //////////////
         
     ///////////jumpforce  gravityyo
       if (isColliding){
       playerrig.velocity = targetDirection.normalized * speednow;   
       }
       ///
      }
     
     /////////////////////
     void Update(){
           MoveInDirectionOfInput();
     }
     /////////////////////////
     // Collision detection.
     
     private void OnCollisionStay(Collision collision){
         isColliding = true;
         jump = false;   
     }
     private void OnCollisionExit(Collision collision){
         isColliding = false;
         //GetComponent<CapsuleCollider>().material.dynamicFriction = 0.6f;
         //GetComponent<CapsuleCollider>().material.staticFriction = 0.6f;
     }
 }
 
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

Answer by DCordoba · Oct 27, 2020 at 09:51 AM

The problem is that you are setting playerrig.velocity on line 42 before you did the jump mechanic.

You need to set the result force after finish the calculations so move all line 42 at the end of MoveInDirectionOfInput() method.

This will add a small bug, the character can now move while is jumping, to solve this put a if to ingnore the axis input when isColliding is false

Comment
Add comment · Show 10 · 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 VladCastillo · Oct 27, 2020 at 10:26 AM 0
Share

I fix falling and moving. It`s cool. But i don`t understand your end about "to solve this put a if to ingnore the axis input when isColliding is false" and how fix jumping? In code reference if you can.

avatar image DCordoba VladCastillo · Oct 27, 2020 at 10:43 AM 0
Share

line 28, wrap the dir variable assign with a if(isColliding) to avoid read the axis input when you are in the middle of air

some games, like old mario, you can still move the character when you are jumping/falling, is part of the gameplay, but this feels unrealistic in more "serious" games.

    if (isColliding){    
           dir.x = Input.GetAxis("Horizontal");
           dir.z = Input.GetAxis("Vertical");
    }


avatar image VladCastillo · Oct 27, 2020 at 10:59 AM 0
Share

All good but Player can`t jump

 using UnityEngine;
 
 // $$anonymous$$oveBehaviour inherits from GenericBehaviour. This class corresponds to basic walk and run behaviour, it is the default behaviour.
 public class Hannamove: $$anonymous$$onoBehaviour
 {
     public float speednow = 1.0f;
     public float walkSpeed = 2.0f;                // Default sprint speed.
     public float runSpeed = 2.0f;                // Default sprint speed.
     public string jumpButton = "Jump";              // Default jump button.
     public float jumpnow = 10f;
     public float jumpforce = 10f;          // Default horizontal inertial force when jumping.
     public float gravityyo = 10f;
     private Vector3 dir = Vector3.zero;
 //private
     public bool jump;                              // Boolean to deter$$anonymous$$e whether or not the player started a jump.
     public bool isColliding;                       // Boolean to deter$$anonymous$$e if the player has collided with an obstacle.
     public Rigidbody playerrig;
 
     // Start is always called after any Awake functions.
     void Start(){
         
     }
 
     // Update is used to set features regardless the active behaviour.
     //////////////////////
    public void $$anonymous$$oveInDirectionOfInput() {
        
   //CharacterController controller = GetComponent<CharacterController>();  
   
     if (isColliding){    
            dir.x = Input.GetAxis("Horizontal");
            dir.z = Input.GetAxis("Vertical");
     }
    Vector3 camDirection = Camera.main.transform.rotation * dir;
    Vector3 targetDirection = new Vector3(camDirection.x, 0f, camDirection.z);
    
    if (dir != Vector3.zero) {
       transform.rotation = Quaternion.Slerp(
       transform.rotation,
       Quaternion.LookRotation(targetDirection),
       Time.deltaTime * speednow);
       ///
    }
     
         if (isColliding && Input.GetButtonDown(jumpButton))   {  
             jump = true;
             jumpnow = jumpforce;    
             targetDirection.y = jumpforce;                
         }
         
         ///
         
         targetDirection.y -= jumpforce * Time.deltaTime;
         
     ///////////jumpforce  gravityyo
       if (isColliding){
       playerrig.velocity = targetDirection.normalized * speednow;   
       }
       ///
      }
     
     /////////////////////
     void Update(){
           $$anonymous$$oveInDirectionOfInput();
     }
     /////////////////////////
     // Collision detection.
     
     private void OnCollisionStay(Collision collision)
     {
         isColliding = true;
         jump = false;   
     }
     private void OnCollisionExit(Collision collision)
     {
         isColliding = false;
         //GetComponent<CapsuleCollider>().material.dynamicFriction = 0.6f;
         //GetComponent<CapsuleCollider>().material.staticFriction = 0.6f;
     }
 }
 

  



avatar image VladCastillo VladCastillo · Oct 27, 2020 at 11:04 AM 0
Share

alt text For context

avatar image DCordoba VladCastillo · Oct 27, 2020 at 11:10 AM 0
Share

ahhh, remove if (isColliding) wrapper form the last one, you need to update this one even on the middle of air...

change

    if (isColliding){
           playerrig.velocity = targetDirection.normalized * speednow;   
    }

just by

   playerrig.velocity = targetDirection.normalized * speednow;   
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

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

Related Questions

zombies and the player SUDDENLY(not a beginning but a middle of playing) falling down 1 Answer

Adding falling and landing animations to the Locomotion System 1 Answer

falling through the terrain 0 Answers

Endless "Falling in place" Simulation Question 0 Answers

Adding Jumping and Falling Animations 0 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