• 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 corderoryan4 · Nov 13, 2021 at 08:33 PM · movement scriptjumping3rd person controller

How do you jump and keep moving to the direction your player/character was when they were on the ground, using a character controller?

I'm gonna try to keep it short. I want to figure out how to make the character/player be able to move in the direction they were once going in while jumping. The reason why is because I don't want the player to let go of the w key and just stop abruptly mid-air, it will look weird, especially if the character is continuing to fall at the same velocity as it was before the key was let go. Also, the other reason is that I'm gonna try to put in a wall jump to my character, and the video that's I'm most likely gonna use to find out how to do that made a point that the character shouldn't be able to turn immediately and "hump the wall".

here's the code for the character:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ThirdPersonMovement : MonoBehaviour
 {
     public CharacterController controller;
     public Transform cam;
     private Vector3 moveVector;
     private Vector3 lastMove;
     private float verticalVelocity;
 
     public float speed = 6f;
     public float gravity = -9.81f;
     public float jumpHeight = -3f;
     public bool _canDoubleJump = false;
     private float airSpeed = 4.0f;
     private float airFriction = 0.65f;
 
     public Transform groundCheck;
     public float groundDistance = 0.4f;
     public LayerMask groundMask;
 
     Vector3 velocity;
     bool isGrounded = false;
 
     public float turnSmoothTime = 0.1f;
     float turnSmoothVelocity;
 
     public float dJumpHeight = 3.5f;
 
     private void Update()
     {
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
 
         if(isGrounded && velocity.y < 0)
         {
             velocity.y = -2.0f;
         }
 
         
         float horizontal = Input.GetAxisRaw("Horizontal");
         float vertical = Input.GetAxisRaw("Vertical");
         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
 
         if (direction.magnitude >= 0.1f)
         {
             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
             transform.rotation = Quaternion.Euler(0f, angle, 0f);
 
             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
             controller.Move(moveDir.normalized * speed * Time.deltaTime);
         }
 
 
         
 
        if (controller.isGrounded == true)
        {
                 _canDoubleJump = false;
                 turnSmoothTime = 0f;
                 controller.slopeLimit = 45.0f;
 
             if (Input.GetButtonDown("Jump"))
             {
 
 
                     velocity.y = Mathf.Sqrt(jumpHeight * gravity * (-2.0f));
                     _canDoubleJump = true;
                     turnSmoothTime = 0.8f;
                     controller.slopeLimit = 90.0f;
 
 
 
 
             }
     
        }
        else
        {
 
 
                 if (Input.GetButtonDown("Jump"))
                 {
                     
 
                     if (_canDoubleJump == true)
                     {
                         velocity.y = Mathf.Sqrt(dJumpHeight * gravity * (-2.0f));
                         _canDoubleJump = false;
                         turnSmoothTime = 0.8f;
                         moveVector = lastMove;
     
                         if (_canDoubleJump == false)
                         {
     
                             turnSmoothTime = 0.8f;
                             moveVector = lastMove;
     
                         }
     
                     }
                 }
        }
 
         
         //if (Input.GetButtonDown("Jump"))
         //{
         //if (controller.isGrounded == false)
         //{
         //velocity.y = Mathf.Sqrt(jumpHeight * gravity * (-2.0f));
         //}
         //else
         //{
 
         //}
         //}
 
         //if (Input.GetButtonDown("Jump"))
         //if (controller.isGrounded)
 
         velocity.y += gravity * Time.deltaTime;
 
         controller.Move(velocity * Time.deltaTime);
 
         if (Input.GetKey("left shift"))
         {
             speed = 16f;
         }
         else
         {
             speed = 8f;
         }
 
         moveVector.y = 0;
         moveVector.Normalize();
         moveVector *= speed;
         moveVector.y = verticalVelocity;
 
         controller.Move(moveVector * Time.deltaTime);
         lastMove = moveVector;
     }
 }


Here's a vid of it happening if you don't understand what I mean: https://www.youtube.com/watch?v=_vkYQHkqbKU

P.S. sorry if this sounds demanding in some way. I've just been trying to figure out how to solve this problem for the past few hours and I just really want to learn how to do it and move on.

P.S.S. I'm a visual learner so please don't try to explain it to me with only words.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

138 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

Related Questions

isGrounded is always false, even with gravity, how do you fix that? 1 Answer

Cannot move and jump with rigid body. 0 Answers

Need help with making a character jump. 1 Answer

Player controller jump not always working 2 Answers

How to stop spam jumping? 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