Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 CyberBlitz55 · Dec 02, 2016 at 06:15 PM · movementmovement scriptmoving

Movement in mid air

So this is my code so far

 using UnityEngine;
 using System.Collections;
 
 public class MovementNuri : MonoBehaviour
 {
     private Vector3 moveVector;
     private Vector3 lastMove;
     private float speed = 5;
     private float verticalVelocity;
     private float gravity = 14.0f;
     private float jumpForce = 10.0f;
     private CharacterController controller;
 
         private void Start()
     {
         controller = GetComponent<CharacterController>();
 
     }
 
     private void Update()
     {
         moveVector.x = Input.GetAxis("Horizontal") * speed;
  
         if (controller.isGrounded)
         {
             verticalVelocity = -gravity * Time.deltaTime;
             if (Input.GetKey(KeyCode.Space))
             {
                 verticalVelocity = jumpForce;
             }
         }    
         else
         {
             verticalVelocity -= gravity * Time.deltaTime;
             moveVector = lastMove;
         }
         
         moveVector.y = 0;
         moveVector.Normalize();
         moveVector *= speed;
         moveVector.y = verticalVelocity;
  
         controller.Move(moveVector * Time.deltaTime);
         lastMove = moveVector;            
     }
 }


So right now I have it set up so that if I'm moving left or right the momentum will carry on to the jump. But how can I make it so that I can make it so I can still have movement in mid air?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by JC_SummitTech · Dec 03, 2016 at 07:46 AM

Basically, remove line 35.

Comment
Add comment · Show 3 · 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
avatar image CyberBlitz55 · Dec 11, 2016 at 05:22 PM 0
Share

Thank you that worked but I also wanted to still have momentum carried over from running to a jump, so when you let go of the button it doesn't stop in mid air. How would I do that?

avatar image JC_SummitTech CyberBlitz55 · Dec 12, 2016 at 02:07 PM 0
Share

You could use the last$$anonymous$$ove value, but at line 35 ins$$anonymous$$d of setting your move to last$$anonymous$$ove, just use part of it, use moveVector + (last$$anonymous$$ove * 0.5f).

By the way, I notice you are multiplying your x speed twice, I don't think the line 22 should be multiplied by speed for your needs.

avatar image JC_SummitTech JC_SummitTech · Dec 12, 2016 at 03:20 PM 0
Share

Also di$$anonymous$$ish your moveVector value while you are in the air, because you probably don't want your momentum to be constant. Something like last$$anonymous$$ove = last$$anonymous$$ove * (1f- Time.deltaTime); Adjust to your needs.

avatar image
0

Answer by Konomira · Dec 11, 2016 at 11:23 PM

Move line 22moveVector.x = Input.GetAxis("Horizontal") * speed; to insideif(controller.isGrounded){ }

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
avatar image
0

Answer by Farmerfromtexas · Apr 25, 2020 at 06:18 PM

@CyberBlitz55 I know this is an old post but this might be the solution you were looking for:

     public float airSpeedModifier = 0.05f;  //The higher the modifier value the more air control where 0 = no air control, and 1 = full air control. 
     
     private void FixedUpdate()
     {
         if (!controller.isGrounded)
         {
             moveVector.x = Mathf.Lerp(lastMove.x, moveVector.x, airSpeedModifier);
             moveVector.z = Mathf.Lerp(lastMove.z, moveVector.z, airSpeedModifier);
         }
      }

In the case of the original post just replace line 35 with the two lines inside the if statement above.

Hope it helps (someone)! :)


Comment
Add comment · Show 1 · 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
avatar image CyberBlitz55 · May 18 at 09:04 AM 0
Share

Unfortunately I have moved on to completely different projects but the help is greatly appreciated :)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

move enemy to old position of player 2 Answers

Make character move left or right only if it is in the air,Make character move left or right only in the air. 0 Answers

How do i maintain the same speed in the air? 1 Answer

Altering movement depending on camera rotation 0 Answers

Mouse move object according to grid 3 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