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 JrizzleisDizzle · Sep 20, 2015 at 06:50 PM · movementscriptingbasicsbasic programming

Need help limiting Air Movement

I'm making an fps and I need help with the movement. Everything seems to be working fine except I'm trying get it to where the player keeps their momentum after they jump while also being able to control their movement in air slightly, any help would be awesome as I'm new to scripting, also if there's anything I should change just let me know please. (:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     CharacterController cc;
 
     float xSpeed;
     float zSpeed;
 
     Vector3 moveVector;
 
     float vVelocity;
 
     public float gravity = 1F;
     public float moveSpeed = 7f;
     public float jumpSpeed = 7;
     public float runMulti = 1.5f;
 
     public string jump = "Jump1";
     public string horizontal = "HorizontalPad1";
     public string vertical = "VerticalPad1";
     public string run = "Run1";
 
     private CollisionFlags m_CollisionFlags;
     private CharacterController m_CharacterController;
 
     // Use this for initialization
     void Start ()
     {
 
         cc = GetComponent<CharacterController>();
     
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetButton(run))
             Run();
         else
         {
             Walk();
         }
     }
 
     void Walk()
     {
 
         xSpeed = Input.GetAxis(horizontal);
         zSpeed = Input.GetAxis(vertical);
 
         moveVector = new Vector3(xSpeed, 0, zSpeed);
         moveVector = transform.TransformDirection(moveVector) * moveSpeed;
 
         vVelocity += Physics.gravity.y * gravity * Time.deltaTime;
 
 
         if (cc.isGrounded)
         {
             if (Input.GetButtonDown(jump))
             {
                 vVelocity = jumpSpeed;
             }
         }
 
         if (!cc.isGrounded)
         {
             moveVector = new Vector3(xSpeed, 0, zSpeed);
             moveVector = transform.TransformDirection(moveVector) * moveSpeed;
         }
 
         moveVector.y = vVelocity;
         m_CollisionFlags = cc.Move(moveVector * Time.deltaTime);
     }
 
     void Run()
     {
         xSpeed = Input.GetAxis(horizontal);
         zSpeed = Input.GetAxis(vertical);
 
         moveVector = new Vector3(xSpeed, 0, zSpeed);
         moveVector = transform.TransformDirection(moveVector) * moveSpeed * runMulti;
 
         vVelocity += Physics.gravity.y * gravity * Time.deltaTime;
 
         if (cc.isGrounded)
         {
             if (Input.GetButtonDown(jump))
             {
                 vVelocity = jumpSpeed;
             }
         }
 
         if (!cc.isGrounded)
         {
             moveVector = new Vector3(xSpeed, 0, zSpeed);
             moveVector = transform.TransformDirection(moveVector) * moveSpeed;
         }
 
         moveVector.y = vVelocity;
         m_CollisionFlags = cc.Move(moveVector * Time.deltaTime);
     }
 
     private void OnControllerColliderHit(ControllerColliderHit hit)
     {
         Rigidbody body = hit.collider.attachedRigidbody;
         //dont move the rigidbody if the character is on top of it
         if (m_CollisionFlags == CollisionFlags.Below)
         {
             return;
         }
 
         if (body == null || body.isKinematic)
         {
             return;
         }
         body.AddForceAtPosition(cc.velocity * 0.1f, hit.point, ForceMode.Impulse);
     }
 
 }
 



Comment
Add comment · Show 4
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 20, 2015 at 09:15 PM 0
Share
  1. the code you posted is just a red mess of nonsense, at least on my computer.

  2. multiply the movement vector by a value below 1.0 while in the air (0.5 would mean half of the normal control).

avatar image JrizzleisDizzle Cherno · Sep 21, 2015 at 12:56 AM 0
Share

@cherno Sorry about that I didn't realize i posted the code incorrectly. I edited the post, I appreciate any help I can get, Thank you.

avatar image Cherno JrizzleisDizzle · Sep 21, 2015 at 10:37 AM 0
Share

Have you tried what I suggested?

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I introduce a turning function for my player in this script? 1 Answer

Modify a script to use buttons instead of keys 1 Answer

Moving gameobject by GUI buttons 1 Answer

Position Text inside Canvas via Script 2 Answers

How to make an object move with a script which isn't attached to it? 2 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