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 /
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 while 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 things (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

Does the isGrounded bool actually change if the player is, you know, not on the ground?

avatar image Benjamin6817 Cherno · Sep 28, 2015 at 09:27 PM 0
Share

Yeah, it's using CharacterController.isGrounded. (player is an instance of CharacterController)

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.

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

Turning to face movement direction 0 Answers

C# Movement Script 3 Answers

How to control an object in a specific area 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