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 kanlam · Apr 14, 2017 at 11:59 PM · unity 5movementcontrollerjumping

Control Air movement with character controller

Hi guys, I am now working on my 3D 3rd person running game now but I have got a big problem on my character controller.

Problem: I want the player can control the character move to the left or right while the player controller is not Onground. I tried to remove if (controller.isGrounded ) statement in the MovementControl() but when I removed it, the character control become not effected by the gravity and cannot jump too. I know the problem is in the MovementControl() but I have already spent one day to fix it but I can't. Can some one please help?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class OceanWorldBMControl : MonoBehaviour {
 
     public Animator anim;
     public CharacterController controller;
     public GameObject water;
 
     public float speed = 6;
     public float sprintSpeed;
     public float currentSpeed; 
     public float jumpSpeed = 8.0f;
     public float gravity = 20.0f;
     public float turnspeed = 10.0f;
     public bool OnGround = true;
     public bool DoubleJumped;
     public bool isSprinting = false;
     public bool isBootsed = false;
     public bool isSwimming = false;
     public bool isPressSprint;
 
 
     public Slider staminaBar;
     public int maxStamina;
     private int staminaFallrate;//how fast it fall
     public int staminaFallMult;
     private int staminaRegainrate;// how fast it recover
     public int staminaReganinMult;
 
     private double boostTime = 20f;
     private float boostedSpeed;
     public float boostTimeFallrate = 1f;
 
     private Vector3 moveDirection = Vector3.zero;
 
 
     // Use this for initialization
     void Start () {
 
         currentSpeed = speed;
         sprintSpeed = speed * 2;
         staminaBar.maxValue = maxStamina;
         staminaBar.value = maxStamina;
 
         staminaFallrate = 1;
         staminaRegainrate = 1;
     }
 
     // Update is called once per frame
     void Update () {
         AnimationControl();
         AirTime ();
         MovementControl ();
         Sprinting ();
     }
 
     void MovementControl()
     {
         /*float rotation = Input.GetAxis ("Horizontal") * turnspeed;
         rotation *= Time.deltaTime;
         transform.Rotate (0, -rotation, 0);*/
 
         if (controller.isGrounded ) {
             moveDirection = new Vector3 (Input.GetAxis ("Horizontal")*speed, 0, Input.GetAxis ("Vertical") * currentSpeed);
             moveDirection = transform.TransformDirection (moveDirection);
         }
 
         if (OnGround && Input.GetButtonDown ("Jump")) {
             moveDirection.y = jumpSpeed;
         }
 
         if (!OnGround && Input.GetButtonDown ("Jump") && !DoubleJumped) {
             moveDirection.y = jumpSpeed;
             DoubleJumped = true;
         }
 
         moveDirection.y -= gravity*Time.deltaTime;        
         controller.Move (moveDirection * Time.deltaTime);
     }
 
     void AnimationControl()
     {
         if (OnGround && Input.GetButtonDown ("Jump")) {
             anim.SetTrigger ("isJumping");
         } 
 
         if (!OnGround && Input.GetButtonDown ("Jump") && !DoubleJumped) {
             anim.SetTrigger ("DoubleJump");
 
         }
         if (controller.isGrounded) {
             anim.SetBool ("isAir", false);        
         } 
         else {
             anim.SetBool ("isAir", true);
         }
         if (Input.GetKey (KeyCode.S)) {
             anim.SetBool ("isRunning", true);    
         } 
         else {
             anim.SetBool ("isRunning", false);
         }
         if (staminaBar.value > 0 && isSprinting || isBootsed) {
             anim.SetBool ("isSprinting", true);
         } 
         else {
             anim.SetBool ("isSprinting", false);
         }
         if (Input.GetKey (KeyCode.W)) {
             anim.SetBool ("isBack", true);    
         } 
         else {
             anim.SetBool ("isBack", false);
         }
         if (isSwimming) {
             anim.SetBool ("isSwimming", true);
         } else {
             anim.SetBool ("isSwimming", false);
         }
     }
 
     void AirTime()
     {
         if (!controller.isGrounded) {
             OnGround = false;
         } else {
             OnGround = true;
             DoubleJumped = false;
         }
     }
 
     void Sprinting(){
 
         if (Input.GetKey (KeyCode.P)) {
             isPressSprint = true;
         } else {
             isPressSprint = false;
         }
 
         if (Input.GetKey (KeyCode.S) && Input.GetKey(KeyCode.P) && !isSwimming) {
             isSprinting = true;
         } 
         else {
             isSprinting = false;
         }
 
         if (staminaBar.value >= maxStamina) {
             staminaBar.value = maxStamina;
 
         } else if (staminaBar.value <= 0) {
             isSprinting = false;
             staminaBar.value = 0;
             currentSpeed = speed;
         }
 
         if (!isSprinting && !isBootsed) {
             currentSpeed = speed;
             staminaBar.value += Time.deltaTime / staminaRegainrate * staminaReganinMult;
         }
 
         if (isSprinting && !isBootsed && isPressSprint) {
             currentSpeed = sprintSpeed;
             staminaBar.value -= Time.deltaTime / staminaFallrate * staminaFallMult;
         }
 
         if (isSprinting && isBootsed ) {
             currentSpeed = sprintSpeed;
             staminaBar.value -= Time.deltaTime / staminaFallrate * staminaFallMult;
             boostTime -= Time.deltaTime * boostTimeFallrate;
             if (boostTime <= 0) {
                 isBootsed = false;
                 currentSpeed = speed;
                 boostTime = 20f;
             }
         }
         if (!isSprinting && isBootsed) {
             currentSpeed = sprintSpeed;
             staminaBar.value += Time.deltaTime / staminaRegainrate * staminaReganinMult;
             boostTime -= Time.deltaTime * boostTimeFallrate;
             if (boostTime <= 0) {
                 isBootsed = false;
                 currentSpeed = speed;
                 boostTime = 20f;
             }
         }
 
         if (isSprinting && !isPressSprint) {
             staminaBar.value += Time.deltaTime / staminaRegainrate * staminaReganinMult;
         }
     }
 
     void OnTriggerEnter(Collider other){
         if (other.gameObject.tag == "water") {
             isSwimming = true;
             Debug.Log ("inWater");
         } 
     } 
     void OnTriggerExit(){
         isSwimming = false;
     }
 }
 
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

170 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

Related Questions

Look rotation viewing vector is zero & diagonal boosting 0 Answers

Issues Getting my GameObject to both Move Forward and Jump properly. 0 Answers

How can I make it so that the longer you hold down the jump button the higher the player jumps? 0 Answers

First person controls for GoogleCardboard 2 Answers

Air Control While 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