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 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.

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

Problem with the Character Controller on the Y-Axis,Character Controller drifting in the Y Axis 0 Answers

Boat Controller Help 0 Answers

Problem with game objects moving relative to the camera Y axis? [ANSWERED] 1 Answer

Help with exception to DontDestroyOnLoad 2 Answers

I want to make an on click movement script with a grid type map need references to look at 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