Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TH3HCK3ABL3 · Jul 18, 2013 at 11:25 PM · speedmomentumairstrafe

How can I "nerf" air-strafe-ing in this script?

I want to be able to stop the Character Controller from continuing to move at the same rate in the air. Basically, lock it to whatever direction momentum during the jump and not allow you to suddenly go backwards from the direction you're currently going.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(CharacterController))]
 public class P1Script: MonoBehaviour {
     public float nSpeed = 5; // Units per second
     public float speedM = .5f; // Multiplies the "nSpeed" variable to increase movement speed
     public float airSpeed = 1.5f; // Multiplies the "cAirSpeed" variable while in the air 
     private float cSpeed; // The current Speed
     public float turnSpeed = 5.0f; // Degrees per second
     public float jumpSpeed = 8; // Height of Jump
     public float gravity = 9.8f; // The rate at which gravity pulls down.
     private float vSpeed = 0 ; // Current Vertical Velocity 
     float vRotation = 0; // Prevents Pitch(?)
     public float yRange = 60.0f; // The angle limit for the Y-Axis look
     CharacterController cController;
     
     
     // Use this for initialization
     void Start () {
         Screen.lockCursor = true; // Locks Cursor to Screen; makes invisible
         cController = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update () {
         
         //Jump
             if(cController.isGrounded) {
             vSpeed = 0; // Grounded character has vSpeed = 0...
         if(Input.GetButton("Jump")) {
                 vSpeed = jumpSpeed;
                 var cAirSpeed = cSpeed / 2;
                 cSpeed = cAirSpeed;
             }    
         }
         
         // Rotation and Walk
         float rotLeftRight = Input.GetAxis("Mouse X");
         transform.Rotate(0, rotLeftRight, 0);
         var vel = transform.forward * Input.GetAxis("Vertical") * cSpeed;
         vRotation -= Input.GetAxis("Mouse Y");
         vRotation = Mathf.Clamp(vRotation, -yRange, yRange);
         Camera.main.transform.localRotation = Quaternion.Euler(vRotation, 0, 0);
         var hor = transform.right * Input.GetAxis("Horizontal") * cSpeed;
         vSpeed -= gravity * Time.deltaTime; // Applies gravity acceleration to the vertical speed
         vel.y = vSpeed; // include vertical speed in the vel
         cController.Move (vel * Time.deltaTime); // Convert vel to displacement and move the character
         hor.y = vSpeed; // Applies gravity acceleration to the horizontal speed:
         cController.Move (hor * Time.deltaTime); // Convert hor to displacement and move the character
         
         //Sprinting
         if (Input.GetButton("Sprint")) { // Check for "Sprint" as defined by project inputs
             cSpeed = nSpeed * speedM;
         } else {
             cSpeed = nSpeed;
         }
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Jul 18, 2013 at 11:43 PM

Try replacing line 41 with this:

 var vel : Vector3;
 if (!cController.isGrounded)
     vel = transform.forward * cAirSpeed;
 else
     vel = transform.forward * Input.GetAxis("Vertical") * cSpeed;
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 TH3HCK3ABL3 · Jul 19, 2013 at 03:50 AM 0
Share

Thank you so much! Just had to write that in C# for my script and it functions perfectly after I made cAirSpeed a normal Variable. Thank you again!

avatar image
0

Answer by Slobdell · Jul 18, 2013 at 11:42 PM

Put a collider on the ground or whatever he's running on and if he's off the ground, keep him moving at the same speed. Basically, use a RayCast and check distance to the ground and only allow controls if he's within a certain distance. Or only allow movement controls if the player and the ground collider are colliding.

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 TH3HCK3ABL3 · Jul 19, 2013 at 03:51 AM 0
Share

This sounds really useful. I will have to look into it. Probable for applying it to walls! Thank for your help!

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

16 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

Related Questions

Speed updated from another script, but doesn't persist 1 Answer

How to make object lag behind camera? 1 Answer

Make an object move towards another object when button is held then drift when button is released 0 Answers

Adding simple strafing to my character controller script... 0 Answers

Inspection camera script problem 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