- Home /
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 );
}
}
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.
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.
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.
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
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.
Your answer
Follow this Question
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