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 hunter2064 · Jul 17, 2018 at 01:22 AM · unity 5movementfpsfps controllerair

Movement in Mid Air WITH a reduced speed

I'm trying to emulate Half Life's movement, and in that game you can influence your movement in mid air, but only to a certain degree. I'm having a hard time doing that. You'll see also that I have 3 different speeds, one for the base movement, and then one to be removed from the base when the player is crouching, and one to be added while the player is sprinting. I'm sure there are better ways to do what I've done, but I haven't messed with it yet sadly. CURRENTLY, the player maintains their momentum JUST before the jump, and keeps that momentum in mid air. The problem is that I don't know how to influence my direction and velocity in mid air while keeping gravity normal. Below is my movement code. I appreciate any and all help. using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float crouchSpeedToRemove = -3.0f; public float baseSpeed = 6.0F; public float runSpeedToAdd = 3.0F; public float jumpForce = 8.0F; public float gravity = 20.0F; /// <summary> /// The height of the character when not crouched. /// </summary> public float regularHeight; public float crouchHeight; private Vector3 moveDirection = Vector3.zero; private Vector3 runDirection = Vector3.zero; private Vector3 crouchDirection = Vector3.zero; private CharacterController charController; private bool isCrouched; void Start() { charController = GetComponent<CharacterController>(); } void Update() { if (charController.isGrounded) { moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= baseSpeed; runDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); runDirection = transform.TransformDirection(runDirection); runDirection *= runSpeedToAdd; crouchDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); crouchDirection = transform.TransformDirection(crouchDirection); crouchDirection *= crouchSpeedToRemove; if (Input.GetButton("Jump"))//jump { moveDirection.y = jumpForce; } if (Input.GetKey(KeyCode.LeftShift) && !isCrouched)//sprint { charController.Move(runDirection * Time.deltaTime); } }//end of isGrounded if (Input.GetKey(KeyCode.LeftControl)) { charController.Move(crouchDirection * Time.deltaTime); charController.height = crouchHeight; isCrouched = true; } else { charController.height = regularHeight; isCrouched = false; } if (Input.GetKey(KeyCode.LeftShift) && !isCrouched)//sprint { charController.Move(runDirection * Time.deltaTime); } moveDirection.y -= gravity * Time.deltaTime;//subtracts gravity from the Y vector in the moveDirection vector 3 charController.Move(moveDirection * Time.deltaTime);//applies new gravity } }

Comment
Add comment · Show 1
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 hunter2064 · Jul 17, 2018 at 01:25 AM 0
Share

PS i have no idea why my unity code is getting squished like it is, in the post preview it doesn't squish it at all!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by yoyo696 · Jul 23, 2018 at 10:36 AM

Chek das ouf: but only to a certain degree. I'm having a hard time doing that. You'll see also that I have 3 different speeds, one for the base movement, and then one to be removed from the base when the player is crouching, and one to be added while the player is sprinting. I'm sure there are better ways to do what I've done, but I haven't messed with it yet sadly. CURRENTLY, the player maintains their momentum JUST before the jump, and keeps that momentum in mid air. The problem is that I don't know how to influence my direction and velocity in mid air while keeping gravity normal. Below is my movement code. I appreciate any and all help. using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float crouchSpeedToRemove = -3.0f; public float baseSpeed = 6.0F; public float runSpeedToAdd = 3.0F; public float jumpForce = 8.0F; public float gravity = 20.0F; /// <summary> /// The height of the character when not crouched. /// </summary> public float regularHeight; public float crouchHeight; private Vector3 moveDirection = Vector3.zero; private Vector3 runDirection = Vector3.zero; private Vector3 crouchDirection = Vector3.zero; private CharacterController charController; private bool isCrouched; void Start() { charController = GetComponent<CharacterController>(); } void Update() { if (charController.isGrounded) { moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= baseSpeed; runDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); runDirection = transform.TransformDirection(runDirection); runDirection *= runSpeedToAdd; crouchDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); crouchDirection = transform.TransformDirection(crouchDirection); crouchDirection *= crouchSpeedToRemove; if (Input.GetButton("Jump"))//jump { moveDirection.y = jumpForce; } if (Input.GetKey(KeyCode.LeftShift) && !isCrouched)//sprint { charController.Move(runDirection * Time.deltaTime); } }//end of isGrounded if (Input.GetKey(KeyCode.LeftControl)) { charController.Move(crouchDirection * Time.deltaTime); charController.height = crouchHeight; isCrouched = true; } else { charController.height = regularHeight; isCrouched = false; } if (Input.GetKey(KeyCode.LeftShift) && !isCrouched)//sprint { charController.Move(runDirection * Time.deltaTime); } moveDirection.y -= gravity * Time.deltaTime;//subtracts gravity from the Y vector in the moveDirection vector 3 charController.Move(moveDirection * Time.deltaTime);//applies new gravity } }

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

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

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

WebGl Mouse Look Problem 0 Answers

MY FPSE`S fps is not moving need help! I tried all solutions! 0 Answers

Carry forward velocity against mouse movements 1 Answer

Shooting does not affect the Health 0 Answers

Player Falls Much Slower While Moving 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