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 aqsanadeem82 · May 14, 2019 at 08:23 AM · unity 5animationscharacter controllerc# tutorialjittering

Character having character controller is very jittery/shaking

My character is having a character controller which is very jittery while movement. i tried tweaking character controller values but nothing worked for me. It is an infinite runner game where the player moves and performs certain animations while moving as the player is standing there is no shaking but as it starts moving it jitters badly.

 public class PlayerMotor : MonoBehaviour
 {
 public static PlayerMotor Instance { get; set;}
 private const float LANE_DISTANCE = 2.5f;
 private const float TURN_SPEED = 0.05f;
 public GameObject Enemy;
 public GameObject StakeBoard;
   
 private bool isRunning = false;
 public bool magnetAttract = false;
 public bool Skateboarding = false;
 public bool SkateValue = true;

 //Animation
 [HideInInspector]
 public Animator anim;

 //Movement
 private CharacterController controller;
 private float jumpForce = 10.0F;   // how high player can jump
 private float gravity = 12.0f;    //Gravity makes the body fall down
 private float verticalVelocity;//This will be set for every single frame

 private int desiredLane = 1;   // 0=Left , 1= Middle , 2=Right

 //Speed Modifier
 private float originalSpeed = 8.0f;
 private float Speed = 8.0f;    // Player Speed
 private float speedIncreaseLastTick;   // Last time speed was increased
 private float speedIncreaseTime = 2.5f;
 private float speedIncreaseAmount = 0.1f;

 
 
 private void Start()
 {
    
     Instance = this;
     
     Speed = originalSpeed;
     controller = GetComponent<CharacterController>();
     anim = GetComponentInChildren<Animator>();
 }

 private void Update()
 {
     if (!isRunning)
     return;
     
 if (Skateboarding == true && SkateValue==true)
     {
         anim.SetBool("Skate", true);
         StakeBoard.SetActive(true);
         SkateValue = false;
     }

     if (Time.time - speedIncreaseLastTick > speedIncreaseTime)
     {
         speedIncreaseLastTick = Time.time;
         Speed += speedIncreaseAmount;
         GameManager.Instance.UpdateModifier(Speed-originalSpeed);
     }


     //Gather the inputs on which lane we should be
     if (MobileInput.Instance.SwipeLeft)
         MoveLane(false);
     if (MobileInput.Instance.SwipeRight)
         MoveLane(true);


     // Calculate Where we should be in the future
     Vector3 targetPosition = transform.position.z * Vector3.forward;
     if (desiredLane == 0)
         targetPosition += Vector3.left * LANE_DISTANCE;
     else if (desiredLane == 2)
         targetPosition += Vector3.right * LANE_DISTANCE;

     // Calculate our move dalta
     Vector3 moveVector = Vector3.zero;
     moveVector.x = (targetPosition - transform.position).normalized.x
 * Speed;  //where we should be - where we are right now  
 , directional vector


 bool isGrounded = IsGrounded();
     anim.SetBool("Grounded", isGrounded);


     // Calculate Y
     if (IsGrounded()) //if Grounded
     {
         verticalVelocity = -0.1f;
        
         if (MobileInput.Instance.SwipeUp)
         {
             //Jump
             if (Skateboarding == true)
             {
                 anim.SetBool("SkateJump", true);
             }else
             anim.SetBool("Jump", true);

             verticalVelocity = jumpForce;
            
             Invoke("Stopjump", 1.0f);

         }
         else if (MobileInput.Instance.SwipeDown)
         {
             //Slide
             StartSliding();
             Invoke("StopSliding", 1.0f);
             FindObjectOfType<AudioManager>().Play("slide");
         }
         
     }
     
     else
     {
         verticalVelocity -= (gravity * Time.deltaTime);

         //Fast falling Mechanics while in the air
         if(MobileInput.Instance.SwipeDown)
         {
             verticalVelocity = -jumpForce;
         }

     }
     moveVector.y = verticalVelocity;
     moveVector.z = Speed;

     //Move the player
     controller.Move(moveVector * Time.deltaTime);

     //Rotate the player to where he is going
     Vector3 dir = controller.velocity;
     if(dir!= Vector3.zero)
     {
         dir.y = 0;
         transform.forward = Vector3.Lerp(transform.forward, dir
     , TURN_SPEED);
     }

   
 }

 private void StartSliding()
 {
     if (Skateboarding == true)
     {
         anim.SetBool("SkateSlide", true);
        
     }
     else
         anim.SetBool("Sliding", true);

     controller.height /= 2;
     controller.center = new Vector3(controller.center.x, 
 controller.center.y/2,controller.center.z);
 }

 private void StopSliding()
 {
     if (Skateboarding == true)
     {
         anim.SetBool("SkateSlide", false);
     }
     else
         anim.SetBool("Sliding", false);

     controller.height *= 2;
     controller.center = new Vector3(controller.center.x, 
 controller.center.y * 2, controller.center.z);
 }
 private void Stopjump()
 {
     if (Skateboarding == true)
     {
         anim.SetBool("SkateJump", false);
     }
     else
         anim.SetBool("Jump", false);
    // controller.height *= 2;
  //   controller.center = new Vector3(controller.center.x, 
 controller.center.y * 2, controller.center.z);
 }
 public void MoveLane(bool goingRight)
 {

     desiredLane += (goingRight) ? 1 : -1;
     desiredLane = Mathf.Clamp(desiredLane, 0, 2);

     // Same As above to check on which lane palyer is 
     //if (!goingRight)
     //{
     //    desiredLane--;
     //    if(desiredLane==-1)
     //        desiredLane = 0;
     //}
     //else
     //{
     //    desiredLane++;
     //    if(desiredLane==3)
     //        desiredLane = 2;
     //}
 }

 private bool IsGrounded()
 {
     Ray groundray = new Ray(
     new Vector3(
     controller.bounds.center.x,
     (controller.bounds.center.y - controller.bounds.extents.y) + 0.02f,
     controller.bounds.center.z),
     Vector3.down);
     Debug.DrawRay(groundray.origin, groundray.direction, Color.cyan,
     1.0f);

     return Physics.Raycast(groundray, 0.2f + 0.1f);
         
 }

 public void StartRunning()
 {
     isRunning = true;
     anim.SetTrigger("StartRunning");
 }

 private void Crash()
 {

    
     anim.SetTrigger("Death");
     
   //  enemy.Instance.go();
     isRunning = false;
     GameManager.Instance.OnDeath();
     FindObjectOfType<AudioManager>().Play("dead");

     Enemy.SetActive(true);
 }
 private void OnControllerColliderHit(ControllerColliderHit hit)
 {
   switch (hit.gameObject.tag)
     {
         case "Obstacle":
             Crash();
             break;
     }

       

 }
 private void OnTriggerEnter(Collider other)
 {
     if (other.tag == "scene")
         GameManager.currentLocation.z = 0.0f;
     if(other.tag=="scene2")
         GameManager.currentLocation.z = 200.0f;
     if (other.tag=="scene3")
         GameManager.currentLocation.z = 400.0f;
     if (other.tag=="scene4")
         GameManager.currentLocation.z = 600.0f;
     if (other.tag=="scene5")
         GameManager.currentLocation.z = 800.0f;
    
 }

 }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by harryscott107 · Dec 29, 2020 at 01:13 AM

I had a similar issue. Make sure you are using the same method to update the character position and the camera position.

I had the camera position changing in FixedUpdate(). I had the character position changing in Update().

Changed them both to Update(), and now it moves smoothly.

Comment
Add comment · Show 1 · 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
avatar image
0

Answer by AzureNoir · Jun 25, 2019 at 10:35 PM

@aqsanadeem82 I am having exactly the same problem. seems like we both are following N3K's "Subway Skater" tutorials. I got it fixed for the PC but as soon as I export it to .apk, my player starts jittering on the phone like it used to do on pc earlier. Please let me know if you've got it fixed or found an alternative way. I am from Lahore, Pakistan. Discussed the problem with a couple of game developers but they were failed to give any solutions and it's been around 3 weeks I am stuck at this problem so does my game.

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

Answer by muhndalobaidi · Mar 17, 2020 at 02:16 PM

I had the same problem and I just fix it. if you didnt find the solution yet. just zero out the y axis of the character controller

Comment
Add comment · Show 1 · 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
avatar image qoorbanienay · Jul 26, 2020 at 10:26 AM 0
Share

I hade the samme problem as you, the slution is very easy. Just got to inspector then character controller and incress the height of it for exemple 2.4

avatar image
0

Answer by dbhansen9 · Jul 19, 2021 at 04:15 PM

My solution was to set the skin width (Located in the inspector on the character controller) to 10% the size of the radius. "Two colliders can penetrate each other as deep as their Skin Width. Larger Skin Widths reduce jitter. Low Skin Width can cause the character to get stuck. A good setting is to make this value 10% of the Radius."(https://docs.unity3d.com/Manual/class-CharacterController.html).

Comment
Add comment · Show 1 · 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
avatar image dbhansen9 · Jul 19, 2021 at 04:22 PM 0
Share

This was after slightly increasing the height and zeroing out the y axis so I'm unsure if they are related.

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

207 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

Related Questions

Extreme Jittering of character having character controller in unity 0 Answers

Character is jittering having character controller in endless runner game 0 Answers

Possible bug (Twitchy Animations) with Unity 5.3.4f1 & Mixamo characters?!? 0 Answers

Character controler Script 1 Answer

My character controller is quite unresponsive, help pls 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