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
-1
Question by oscarkool · Jan 08, 2014 at 11:59 PM · animationmovementjump

How To Lock Direction While Jumping?

So I have this movement script but I want to be able to not move direction while jumping. At the moment, I can fly in any direction. I'm brand new to C# so obviously I have no idea, it's all a foreign language to me. Let me know what I need to change, thanks:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     
     // This component is only enabled for "my player" (i.e. the character belonging to the local client machine).
     // Remote players figures will be moved by a NetworkCharacter, which is also responsible for sending "my player's"
     // location to other computers.
     
     public float speed = 10f;        // The speed at which I run
     public float jumpSpeed = 6f;    // How much power we put into our jump. Change this to jump higher.
     
     // Booking variables
     Vector3 direction = Vector3.zero;    // forward/back & left/right
     float   verticalVelocity = 0;        // up/down
     
     CharacterController cc;
     Animator anim;
     
     // Use this for initialization
     void Start () {
         cc = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
         
         // WASD forward/back & left/right movement is stored in "direction"
         direction = transform.rotation * new Vector3( Input.GetAxis("Horizontal") , 0, Input.GetAxis("Vertical") );
         
         // This ensures that we don't move faster going diagonally
         if(direction.magnitude > 1f) {
             direction = direction.normalized;
         }
         
         // Set our animation "Speed" parameter. This will move us from "idle" to "run" animations,
         // but we could also use this to blend between "walk" and "run" as well.
         anim.SetFloat("Speed", direction.magnitude);
         
         // If we're on the ground and the player wants to jump, set
         // verticalVelocity to a positive number.
         // If you want double-jumping, you'll want some extra code
         // here instead of just checking "cc.isGrounded".
         if(cc.isGrounded && Input.GetButton("Jump")) {
             verticalVelocity = jumpSpeed;
         }
     }
     
     // FixedUpdate is called once per physics loop
     // Do all MOVEMENT and other physics stuff here.
     void FixedUpdate () {
         
         // "direction" is the desired movement direction, based on our player's input
         Vector3 dist = direction * speed * Time.deltaTime;
         
         if(cc.isGrounded && verticalVelocity < 0) {
             // We are currently on the ground and vertical velocity is
             // not positive (i.e. we are not starting a jump).
             
             // Ensure that we aren't playing the jumping animation
             anim.SetBool("Jumping", false);
             
             // Set our vertical velocity to *almost* zero. This ensures that:
             //   a) We don't start falling at warp speed if we fall off a cliff (by being close to zero)
             //   b) cc.isGrounded returns true every frame (by still being slightly negative, as opposed to zero)
             verticalVelocity = Physics.gravity.y * Time.deltaTime;
         }
         else {
             // We are either not grounded, or we have a positive verticalVelocity (i.e. we ARE starting a jump)
             
             // To make sure we don't go into the jump animation while walking down a slope, make sure that
             // verticalVelocity is above some arbitrary threshold before triggering the animation.
             // 75% of "jumpSpeed" seems like a good safe number, but could be a standalone public variable too.
             //
             // Another option would be to do a raycast down and start the jump/fall animation whenever we were
             // more than ___ distance above the ground.
             if(Mathf.Abs(verticalVelocity) > jumpSpeed*0.75f) {
                 anim.SetBool("Jumping", true);
             }
             
             // Apply gravity.
             verticalVelocity += Physics.gravity.y * Time.deltaTime;
         }
         
         // Add our verticalVelocity to our actual movement for this frame
         dist.y = verticalVelocity * Time.deltaTime;
         
         // Apply the movement to our character controller (which handles collisions for us)
         cc.Move( dist );
     }
 }
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

1 Reply

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

Answer by HappyMoo · Jan 09, 2014 at 12:41 AM

Hi,

no need to rush anything... you'll need to learn C# anyway eventually, so start now, so you actually understand what happens in the scripts you copy'n'paste and also can do the changes yourself

Work through all this:

http://unity3d.com/learn/tutorials/modules/beginner/scripting

If you need some things explained from another point of view, you can check this:

http://channel9.msdn.com/Series/C-Sharp-Fundamentals-Development-for-Absolute-Beginners

Because, if you can't figure out the script yet, although it's documented so well, you wouldn't understand the answer anyway.

It's really frustrating for everybody here to get so many questions, which are not really questions, but requests to program the game for you.

Please don't do that. Educate yourself, do some basic research before you throw the code you copied somewhere at our feet.

There are enough resources out there to learn everything you need and you should at least have some basic understanding before posting here.

Comment
Add comment · Show 5 · 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 HappyMoo · Jan 09, 2014 at 12:48 AM 0
Share

Once you understand how the above script works, here's what you need to do:

Store your horizontal velocity in a variable and only update that variable while you are grounded.

avatar image HappyMoo · Jan 09, 2014 at 01:02 AM 0
Share

Well, first of all, in your first version you said you've been doing this for hours... now it's days.

If you'd used that 3 days to go through the Learning $$anonymous$$odules, you wouldn't need to write in all of your questions that you are a total noob and obviously have not the slightest clue, because you didn't spend any time actually trying to learn something before posting here.

This is no script fixing service.

avatar image oscarkool · Jan 09, 2014 at 01:03 AM 0
Share

I meant 3 days working on movement, hours for the jump. Either way, your comment helped and I figured it out so thank you and I'm going to take a break and spend some time learning

avatar image HappyMoo · Jan 09, 2014 at 01:07 AM 0
Share

Then accept the answer and upvote it please. Also, if you haven't seen the tutorial video linked in the right bar on how this site works, give it a watch.

avatar image oscarkool · Jan 09, 2014 at 01:23 AM 0
Share

I would upvote but I'm new, no rep

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

19 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

Related Questions

Animation Cycle Dilemma 0 Answers

why will my jump animation only play on multi-jumps? 0 Answers

Moving during jumping animation 1 Answer

Walking Motion 1 Answer

How to make smooth jump? 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