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 GoodKingJohn · Jul 06, 2017 at 02:15 PM · physicsjumpingplayer movementcharacter controller

Inconsistent jumping with character controller

I've made a simple first person player controller script, which just moves the character so far. The X-Z movement is working fine, but I'm having trouble with an annoying glitchy effect that can happen when jumping. Normally jumping there is no problem. However if I jump to a higher level platform from a lower one, the jumping becomes inconsistent. Every second bounce is very low, much lower than programmed, while the ones in between are fine. This continues until I let go of the space bar, at which point it seems to reset itself and all works well until I jump up a level again. Note that this does not happen when jumping down a level. Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour {
 
     public float speed;
     public float jumpSpeed;
     public float resetSpeed;
     public int health;
     public float gravity;
     Camera myCamera;
     Vector3 myCameraOrigin;
     Vector3 myCameraTarget;
     bool justStopped = false;
     float startTime;
     float journeyLength;
     public Vector3 currentVelocity;
 
     // Use this for initialization
     void Start () {
         myCamera = GetComponentInChildren<Camera> ();
     }
     
     // Update is called once per frame
     void Update () {
         currentVelocity.x = 0;
         currentVelocity.z = 0;
         CharacterController con = GetComponent<CharacterController> ();
         bool moving = false;
         if (Mathf.Abs (Input.GetAxisRaw ("Forward")) > 0) {
             moving = true;
             currentVelocity += Vector3.forward;
         }
         if (Mathf.Abs (Input.GetAxisRaw ("Backward")) > 0) {
             moving = true;
             currentVelocity += Vector3.back;
         }
         if (Mathf.Abs (Input.GetAxisRaw ("Left")) > 0) {
             moving = true;
             currentVelocity += Vector3.left;
         }
         if (Mathf.Abs (Input.GetAxisRaw ("Right")) > 0) {
             moving = true;
             currentVelocity += Vector3.right;
         }
         if (Mathf.Abs (Input.GetAxisRaw ("Jump")) > 0 && con.isGrounded) {
             currentVelocity.y += jumpSpeed * Time.deltaTime;
         }
         if (moving) {
             float velocitySpeed = speed * Time.deltaTime;
             Vector3 moveDirection = myCamera.transform.TransformDirection (currentVelocity);
             moveDirection.y = 0;
             moveDirection.Normalize ();
             moveDirection *= velocitySpeed;
             currentVelocity.x = moveDirection.x;
             currentVelocity.z = moveDirection.z;
             justStopped = false;
             //myCamera.transform.Translate (Vector3.left * 0.01f * Mathf.Sin (Time.time * 5));
             myCamera.transform.Translate (Vector3.up * 0.01f * Mathf.Sin (Time.time * 10));
         }
         if (!con.isGrounded) {
             currentVelocity.y -= gravity * Time.deltaTime;
         } else if (currentVelocity.y < 0) {
             currentVelocity.y = 0;
         }
         con.Move (currentVelocity);
         if (!moving && !justStopped) {
             justStopped = true;
             myCameraOrigin = myCamera.transform.localPosition;
             myCameraTarget = new Vector3 (0, 1, 0);
             startTime = Time.time;
             journeyLength = Vector3.Distance(myCameraOrigin, myCameraTarget);
         } else if (!moving) {
             float distCovered = (Time.time - startTime) * resetSpeed;
             float fracJourney = distCovered / journeyLength;
             myCamera.transform.localPosition = Vector3.Lerp (myCamera.transform.localPosition, myCameraTarget, fracJourney);
         }
         transform.rotation = Quaternion.Euler (0, myCamera.transform.rotation.y, 0);
     }
 }
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
1
Best Answer

Answer by GoodKingJohn · Jul 06, 2017 at 03:46 PM

I solved the issue by moving the jump check to add jumping force after the isGrounded check, so that if a jump was initiated it is not immediately negated, as I think is what was happening.

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

109 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

Related Questions

Help with Character Controller Platform Physics 0 Answers

Gun collision problem 3 Answers

Non slippery movement 1 Answer

Character Physics 0 Answers

2d platfromer player controller will not jump 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