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 metalhero93 · Jan 09, 2020 at 12:49 PM · jumpingcharacter controllercharacter movement

Jump while running not faster than normal jump. Any help with the code?

Hi everyone!

I found and adapted this piece of code to create a simple character controller. Everything works as I need except for jump: if the character walks (or stands still) and then jump there are no problems but if I make it run and then press the jump button, the jump movement is identical to when it walks. I expect him to have a longer and faster jump but it doesn't happen!

Here's the code (please ignore everything related to the animator):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerControllerMouseNOROOT : MonoBehaviour
 {
 public CharacterController controller;
 private Animator anim;
 private float x = 0.0f;
 private float z = 0.0f;
 
 [SerializeField] private float walkSpeed = 2.0f;
 [SerializeField] private float runSpeed = 6.0f;
 [SerializeField] private float jumpHeight = 3.0f;
 [SerializeField] private float rotSpeed = 4.0f;
 
 private float speed = 0.0f;
 private float rotation = 0.0f;
 
 [SerializeField] private Transform groundCheck;
 [SerializeField] private float groundDistance = 0.4f;
 [SerializeField] private LayerMask groundMask;
 public bool isGrounded;
 [SerializeField] private float gravity = -19.62f;
 private Vector3 velocity;
 
 // Start is called before the first frame update
 void Start()
 {
 controller = GetComponent<CharacterController>();
 anim = GetComponent<Animator>();
 }
 
 // Update is called once per frame
 void Update()
 {
 // Check to see if player is grounded
 isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
 
 if (isGrounded && velocity.y < 0)
 {
 velocity.y = -2.0f;
 anim.SetBool("isGrounded", true);
 }
 
 // Player movement and sprint
 x = Input.GetAxis("Horizontal");
 z = Input.GetAxis("Vertical");
 rotation = x;
 anim.SetFloat("Rotation", rotation);
 
 if (z != 0f)
 { 
 if (z > 0)
 {
 speed = walkSpeed;
 anim.SetFloat("Speed", 1.0f);
 }
 else
 {
 speed = walkSpeed;
 anim.SetFloat("Speed", -1.0f);
 }
 
 if (Input.GetButton("Sprint") && isGrounded && z > 0)
 {
 speed = runSpeed;
 anim.SetFloat("Speed", 2.0f);
 }
 }
 else
 {
 speed = 0f;
 anim.SetFloat("Speed", 0f);
 }
 
 Vector3 move = Vector3.Normalize(transform.right * x + transform.forward * z);
 
 move *= speed;
 
 velocity.y += gravity * Time.deltaTime;
 
 controller.Move(move * Time.deltaTime);
 
 controller.Move(velocity * Time.deltaTime);
 
 transform.Rotate(0, x * rotSpeed, 0);
 
 // Jump
 if (Input.GetButtonDown("Jump") && isGrounded)
 {
 isGrounded = false;
 velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
 anim.SetTrigger("isJumping");
 anim.SetBool("isGrounded", false);
 }
 
 }
 
 }

Thank you in advance for your help!

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 logicandchaos · Jan 09, 2020 at 01:04 PM

The x velocity when jumping should be the same it was before jumping.. not sure why it wouldn't be.. my guess is you are setting the speed just before or during the jump. if you want jumps to be faster you can increase the x velocity when you jump.. if you multiply it then it won't affect jumping straight up. How did you set velocity.y without creating a new vector?

Here you set speed to walkSpeed regardless of what z equals:

if (z > 0) { speed = walkSpeed; anim.SetFloat("Speed", 1.0f); } else { speed = walkSpeed; anim.SetFloat("Speed", -1.0f); }

You also set grounded to true if the y velocity is negative that means you are grounded as soon as you are falling, so you should be getting multiple jumps.

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

119 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

Related Questions

[SOLVED] 2D Character Controller gains velocity when colliding with a corner 1 Answer

how to make 2d character jump? 0 Answers

,How to disable autojumping when holding spacebar? 4 Answers

Character Controller Upward Movement Along Concave Surface 0 Answers

Shift BHOP in Character Controller 0 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