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 /
  • Help Room /
avatar image
0
Question by LaphicetCrowe · Oct 29, 2020 at 06:13 AM · charactercontrollergravityjump

Gravity randomly does not work

I'm trying to do a third person player movement script using a CharacterController. Ground movement seems good, as does the rotation of the character and camera. However, the ability to jump has plagued me with headaches for days now. I don't know if the gravity just randomly refuses to work (sometimes it takes up to 5 minutes for gravity to "kick in") or if the jumping itself is bugged. It seems more likely to act up when I modify terrain or place new objects in the world.
My script code is as follows, minus a few details unrelated to the problem. It's not shown here, but the Jump function is triggered by an animation event in the jump animation. The character squats first, before it jumps. Additionally, the raycast in FixedUpdate was suggested by my teacher, but I don't see any difference in behavior. My problem seems to be beyond his scope, though.
A bit of a disclaimer: this is not part of my original lesson, which is a top-down twin stick shooter that I went ahead and turned into an almost full-fledged third person shooter because I wanted to challenge myself further. My teachers encourage me to add my own take on lessons, and for the most part, this works just fine. It's only the jump behavior that I'm having difficulty with. Additionally, the ConsoleReadyBehaviour this inherits from is simply an abstract class extending MonoBehaviour to add a few things unrelated to this problem that I wanted all my components to have.
Also, my apologies if the code seems mashed together. The question editor seems to strip away blank lines.

 using UnityEngine;

 public class PlayerController : ConsoleReadyBehaviour
 {
     [Header("Movement Physics")]
     public float turnSpeed = 10f;
     public float jumpStrength = 1f;
     public float moveSpeedMultiplier = 2f;
     public float gravityScale = -3f;
     public float groundDistanceFactor = 10f;

     Animator animator;
     Quaternion freeRotation;
     Vector3 controllerVelocity;
     Vector3 input;
     CharacterController controller;
     float moveSpeed = 0f;
     float velocity;

     void Awake()
     {
         animator = GetComponent<Animator>();
         controller = GetComponent<CharacterController>();
     }

     void Rotate()
     {
         Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
         forward.y = 0f;
         if (input.z < 0f) forward = -forward;
         Vector3 targetDirection = input.x * Camera.main.transform.TransformDirection(Vector3.right) + input.z * forward;

         bool movement = input != Vector3.zero && targetDirection.magnitude > 0.1f;

         if (movement || Input.GetAxis(GameManager.FIRE_1) == 1f)
         {
             if (movement)
             {
                 Vector3 lookDirection = targetDirection.normalized;
                 freeRotation = Quaternion.LookRotation(lookDirection, transform.up);
             }
             else // fire button
                 freeRotation = Quaternion.LookRotation(Camera.main.transform.forward, transform.up);

             float rotationDifference = freeRotation.eulerAngles.y - transform.eulerAngles.y;
             float eulerY = transform.eulerAngles.y;

             if (rotationDifference < 0f || rotationDifference > 0f) eulerY = freeRotation.eulerAngles.y;
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(new Vector3(0f, eulerY, 0f)), turnSpeed * Time.deltaTime);
         }
     }

     private void FixedUpdate()
     {
         if (controllerVelocity.y < 0f)
             if (Physics.Raycast(transform.position, -Vector3.up, out _, groundDistanceFactor))
                 controllerVelocity.y = 0f;
     }

     void Move()
     {
         controllerVelocity = controller.velocity;

         moveSpeed = Mathf.Clamp(/*input.x + */input.z, -1f, 1f);
         moveSpeed = Mathf.SmoothDamp(animator.GetFloat(GameManager.SPEED_HASH), moveSpeed, ref velocity, 0.1f);
         animator.SetFloat(GameManager.SPEED_HASH, moveSpeed);
         input.z = Mathf.Abs(input.z); // no moonwalking!
         controller.Move(freeRotation * input * moveSpeed * moveSpeedMultiplier * Time.deltaTime); // apply movement

         if (Input.GetMouseButtonDown(2)) animator.SetTrigger(GameManager.JUMP_HASH);

         controllerVelocity.y += Physics.gravity.y * Time.deltaTime;
         controller.Move(controllerVelocity * Time.deltaTime); // apply the gravity
     }

     void Update()
     {
         input = new Vector3(Input.GetAxis(GameManager.HORIZONTAL), 0f, Input.GetAxis(GameManager.VERTICAL));

         Rotate();
         Move();
     }

     public void Jump()
     {
         controllerVelocity.y += Mathf.Sqrt(jumpStrength * gravityScale * Physics.gravity.y);
         controller.Move(controllerVelocity * Time.deltaTime);
     }
 }
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.

Follow this Question

Answers Answers and Comments

236 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Unity 5 Player Control Movement, camera (3rd prs following) jump + gravity 0 Answers

Gravity To CharacterController 0 Answers

Character Controller Inconsistent jump 1 Answer

Sluggish Gravity with Character Controller 0 Answers

Character Controller too lag and no and without equal height 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