• 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 wilsonbruce43 · Feb 01, 2020 at 03:36 AM · charactercontrollermovement scriptcharacter controllercharacter controlling3dplatformtutorial

Movement When Character is in air.

So i'm not the best at implementing new code so I've ran into an issue. I've got a basic character controller that works great other then the fact that i cant control movement of the character while in the air. This is my current code that i'm working with and i just cant find a way to allow the movement in air. Any input is most appreciated.

 public class PlayerMovement : MonoBehaviour
 {
     public PlayerStates playerStates;

     [Header("Inputs")]
     public string HorizontalInput = "Horizontal";
     public string VerticalInput = "Vertical";
     public string RunInput = "Run";
     public string JumpInput = "Jump";

     [Header("Player Motor")]
     [Range(1f,15f)]
     public float walkSpeed;
     [Range(1f,15f)]
     public float runSpeed;
     [Range(1f,15f)]
     public float JumpForce;
     public Transform FootLocation;

     [Header("Animator and Parameters")]
     public Animator CharacterAnimator;
     public float HorzAnimation;
     public float VertAnimation;
     public bool JumpAnimation;
     public bool LandAnimation;

     [Header("Sounds")]
     public List<AudioClip> FootstepSounds;
     public List<AudioClip> JumpSounds;
     public List<AudioClip> LandSounds;

     CharacterController characterController;

     float _footstepDelay;
     AudioSource _audioSource;
     float footstep_et = 0;

     // Use this for initialization
     void Start()
     {
         characterController = GetComponent<CharacterController>();
         _audioSource = gameObject.AddComponent<AudioSource>();
     }

     // Update is called once per frame
     void Update()
     {
         //handle controller
         HandlePlayerControls();

         //sync animations with controller
         SetCharacterAnimations();

         //sync footsteps with controller
         PlayFootstepSounds();
     }

     void HandlePlayerControls()
     {
         float hInput = Input.GetAxisRaw(HorizontalInput);
         float vInput = Input.GetAxisRaw(VerticalInput);

         Vector3 fwdMovement = characterController.isGrounded == true ? transform.forward * vInput : Vector3.zero;
         Vector3 rightMovement = characterController.isGrounded == true ? transform.right * hInput : Vector3.zero;

         float _speed = Input.GetButton(RunInput) ? runSpeed : walkSpeed;
         characterController.SimpleMove(Vector3.ClampMagnitude(fwdMovement + rightMovement, 1f) * _speed);

         if (characterController.isGrounded)
             Jump();

         //Managing Player States
         if (characterController.isGrounded)
         {
             if (hInput == 0 && vInput == 0)
                 playerStates = PlayerStates.Idle;
             else
             {
                 if (_speed == walkSpeed)
                     playerStates = PlayerStates.Walking;
                 else
                     playerStates = PlayerStates.Running;

                 _footstepDelay = (2/_speed);
             }
         }
         else
             playerStates = PlayerStates.Jumping;
     }

     void Jump()
     {
         if (Input.GetButtonDown(JumpInput))
         {
             StartCoroutine(PerformJumpRoutine());
             JumpAnimation = true;
         }
     }

     IEnumerator PerformJumpRoutine()
     {
         //play jump sound
         if (_audioSource)
             _audioSource.PlayOneShot(JumpSounds[Random.Range(0, JumpSounds.Count)]);

         float _jump = JumpForce;

         do
         {
             characterController.Move(Vector3.up * _jump * Time.deltaTime);
             _jump -= Time.deltaTime;
             yield return null;
         }
         while (!characterController.isGrounded);

         //play land sound
         if (_audioSource)
             _audioSource.PlayOneShot(LandSounds[Random.Range(0, LandSounds.Count)]);

     }

     void SetCharacterAnimations()
     {
         if (!CharacterAnimator)
             return;

         switch (playerStates)
         {
             case PlayerStates.Idle:
                 HorzAnimation = Mathf.Lerp(HorzAnimation, 0, 5 * Time.deltaTime);
                 VertAnimation = Mathf.Lerp(VertAnimation, 0, 5 * Time.deltaTime);
                 break;

             case PlayerStates.Walking:
                 HorzAnimation = Mathf.Lerp(HorzAnimation, 1 * Input.GetAxis("Horizontal"), 5 * Time.deltaTime);
                 VertAnimation = Mathf.Lerp(VertAnimation, 1 * Input.GetAxis("Vertical"), 5 * Time.deltaTime);
                 break;

             case PlayerStates.Running:
                 HorzAnimation = Mathf.Lerp(HorzAnimation, 2 * Input.GetAxis("Horizontal"), 5 * Time.deltaTime);
                 VertAnimation = Mathf.Lerp(VertAnimation, 2 * Input.GetAxis("Vertical"), 5 * Time.deltaTime);
                 break;

             case PlayerStates.Jumping:
                 if (JumpAnimation)
                 {
                     CharacterAnimator.SetTrigger("Jump");
                     JumpAnimation = false;
                 }
                 break;
         }

         LandAnimation = characterController.isGrounded;
         CharacterAnimator.SetFloat("Horizontal", HorzAnimation);
         CharacterAnimator.SetFloat("Vertical", VertAnimation);
         CharacterAnimator.SetBool("isGrounded", LandAnimation);
     }

     bool onGround()
     {
         bool retVal = false;

         if (Physics.Raycast(FootLocation.position, Vector3.down, 0.1f))
             retVal = true;
         else
             retVal = false;

         return retVal;
     }

     void PlayFootstepSounds()
     {
         if (playerStates == PlayerStates.Idle || playerStates == PlayerStates.Jumping)
             return;

         if (footstep_et < _footstepDelay)
             footstep_et += Time.deltaTime;
         else
         {
             footstep_et = 0;
             _audioSource.PlayOneShot(FootstepSounds[Random.Range(0, FootstepSounds.Count)]);
         }
     }

 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

209 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

Related Questions

Question on Unity5 CharacterController performance/efficiency 0 Answers

I can't rotate and move in the opposite direction with character Controller 0 Answers

Character flies up and gets stuck there, won't move forward? 1 Answer

,Please help me with CharacterController.isGrounded 0 Answers

How do I transfer the momentum from my speed to the player? 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