• 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 Benjamin6817 · Sep 28, 2015 at 06:39 AM · movementcharactercontrollercontrolair

CharacerController not allowing mid-air movement

I am attempting to implement a 3rd person platformer. A key aspect of platforming is the ability to control your character w$$anonymous$$le in the air. I took the code from the Unity documentation on CharacterController.Move and I noticed that if I took the movement part of the code out of the if(player.isGrounded) (so that the player could still move even if he was in the air), I couldn't jump. I tried a lot of t$$anonymous$$ngs (changing values, lerping positions, and lots more) but I couldn't get the jumping working. Here's the code:

 using UnityEngine;
 using System.Collections;

 public enum State
 {
     standing = 0,
     walking = 1,
     running = 2,
     sprinting = 3,
     jumping = 4
 }

 [RequireComponent (typeof(CharacterController))]
 public class MovementController : MonoBehaviour
 {
     public float walkSpeed = 3.0f;
     public float runSpeed = 4.0f;
     public float sprintSpeed = 6.0f
     public float strafeSpeed = 0.80f;

     public float runThreshold = 0.5f;
     public float maxSprintSeconds = 15.0f;
     public float sprintCooldown = 1.0f;
     public float sprintCooldownStart = 5.0f;

     public float jumpForce = 8.0f;
     public float airControl = 3.0f;
     public float gravity = 20.0f;

     private Vector3 moveDirection = Vector3.zero;
     private CharacterController player;
     private float horizontal;
             private float vertical;
             private State currentState = State.standing;

     public void Start ()
     {
         player = GetComponent<CharacterController> ();

         if (!player) {
             Debug.Log ("There is no CharacterController component attached to the \"" + gameObject.name +
                 "\", and the MovementController.cs script depends on it!");
         }
     }

     public void Update ()
     {
         if (!player)
             return;

         horizontal = Input.GetAxis ("Horizontal");
         vertical = Input.GetAxis ("Vertical");

         if (player.isGrounded) {
             if (horizontal == 0 && vertical == 0)
                 currentState = State.standing;
             else if (Mathf.Abs (horizontal) < runThreshold && Mathf.Abs (vertical) < runThreshold)
                 currentState = State.walking;
             else if (Mathf.Abs (horizontal) > runThreshold || Mathf.Abs (vertical) > runThreshold)
                 currentState = State.running;

             if (currentState == State.walking) {
                 moveDirection.Set (horizontal * strafeSpeed * walkSpeed, 0, vertical * walkSpeed);
             }
             if (currentState == State.running) {
                 moveDirection.Set (horizontal * strafeSpeed * runSpeed, 0, vertical * runSpeed);
             }

             if (Input.GetButton ("Jump"))
                 moveDirection.y = jumpForce;
         } else {
             currentState = State.jumping;

             moveDirection.x = horizontal * strafeSpeed * airControl;
             moveDirection.y = vertical * airControl;
         }

         moveDirection = transform.TransformDirection (moveDirection);
         moveDirection.y -= gravity;
         moveDirection *= Time.deltaTime;

         player.Move (moveDirection);
     }

 }


Thanks in advance, for helping me. :)

Comment
Add comment · Show 2
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 Cherno · Sep 28, 2015 at 10:34 AM 0
Share
avatar image Benjamin6817 Cherno · Sep 28, 2015 at 09:27 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Polymo · Sep 28, 2015 at 10:51 AM

Just wrap the IsGrounded only around the Jumpbutton-block.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

In air movement troubles. 1 Answer

Making a character jump with the Character Controller Component instead of Capsule Collider 0 Answers

Turning to face movement direction 0 Answers

Animation not working correctly, character is going up 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