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 Andrelll · Nov 29, 2016 at 08:17 AM · c#scripting problemcharactercontrollerjumpingcharacter controller

Character's jumping mechanism stuck into something invisible

Hello, I was making a 3D sidescroller and I had a problem with the character's jumping mechanism. I have no idea how to solve this issue: Sometimes when the character jumps he gets stuck into something invisible, but if he makes the jump again the jump happens normally. It's difficult to explain it, so it will be easier if you just see the script working: just put it in a cube without a box collider and activate the layer "Ignore Raycast" and you will see it happening

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
     private float inputDirection;    // X Value of MoveVector
     private float verticalVelocity;  // Y Value of MoveVector
 
     private float speed = 5.0f;
     private float gravity = 30.0f;
     private float jumpForce = 10.0f;
     private bool secondJumpAvail = false;
 
     private Vector3 moveVector;
     private Vector3 lastMotion;
     private CharacterController controller;
 
     public AudioClip[] audioClip;
 
     // Use this for initialization
     void Start () {
         controller = GetComponent<CharacterController>();
     }
     void PlaySound(int clip)
     {
         AudioSource audio = GetComponent<AudioSource>();
         audio.clip = audioClip[clip];
         audio.Play();
     }
 
     // Update is called once per frame
     void Update () {
 
         IsControllerGrounded();
        moveVector = Vector3.zero;
        inputDirection = Input.GetAxis("Horizontal") * speed;
 
         if(IsControllerGrounded())
         {
             verticalVelocity = 0;
 
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 // Make the player jump
                 verticalVelocity = jumpForce;
                 secondJumpAvail = true;
             }
                 moveVector.x = inputDirection;
         }
         else
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 if (secondJumpAvail)
                 {
                     verticalVelocity = jumpForce;
                     secondJumpAvail = false; 
                 }
 
             }
             verticalVelocity -= gravity * Time.deltaTime;
             moveVector.x = lastMotion.x;
         }
 
         moveVector.y = verticalVelocity;
         //moveVector = new Vector3(inputDirection, verticalVelocity, 0);
         controller.Move(moveVector * Time.deltaTime);
         lastMotion = moveVector;
 
     
     }
 
     private bool IsControllerGrounded()
     {
         Vector3 lefRayStart;
         Vector3 rightRayStart;
 
         lefRayStart = controller.bounds.center;
         rightRayStart = controller.bounds.center;
 
         lefRayStart.x -= controller.bounds.extents.x;
         rightRayStart.x += controller.bounds.extents.x;
 
         Debug.DrawRay(lefRayStart, Vector3.down, Color.red);
         Debug.DrawRay(rightRayStart, Vector3.down, Color.green);
 
         if (Physics.Raycast(lefRayStart, Vector3.down, (controller.height / 2) + 0.2f))
             return true;
         if (Physics.Raycast(rightRayStart, Vector3.down, (controller.height / 2) + 0.2f))
             return true;
 
         return false;
     }
 
     private void OnControllerColliderHit(ControllerColliderHit hit)
     {
         if(controller.collisionFlags == CollisionFlags.Sides)
         {
             if(Input.GetKeyDown(KeyCode.Space))
             {
                 Debug.DrawRay(hit.point, hit.normal, Color.red, 2.0f);
                 moveVector = hit.normal * speed;
                 verticalVelocity = jumpForce;
                 secondJumpAvail = true;
             }
         }
 
         // Collectables
         switch(hit.gameObject.tag)
         {
             case "Coin":
                 LevelManager.Instance.CollectCoin();
                 Destroy(hit.gameObject);
                 PlaySound(0);
                 break;
             case "Teleporter":
                 transform.position = hit.transform.GetChild(0).position;
                 break;
             case "WinBox":
                 LevelManager.Instance.Win();
                 break;
             default:
                 break;
         }
     }
     
 }
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 conman79 · Nov 29, 2016 at 05:46 PM

I just tested this and the problem seems to be at line 40 "verticalVelocity = 0;" Remove this line and the jump doesn't get stuck any more.

I think what was happening is that sometimes the player would pass slightly under the ground and this line would stop him there, causing the player to be still grounded for a brief period after you next press jump.

You don't need this line anyway because you're already setting moveVector to Vector3.zero every frame,

Comment
Add comment · Show 1 · 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 Andrelll · Nov 30, 2016 at 02:35 AM 0
Share

Worked perfectly! Thank you!

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

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

I have a problem in the jump system. 1 Answer

How do i move a cube like an actual cube?,How Can i Move a Cube? 0 Answers

Raycast fall detection and issues with .isGrounded, 1 Answer

Error CS0120 : An object reference is required to access non-static member 3 Answers

Problem with jumping 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