• 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
Question by mat_capedkoala · Jul 12, 2019 at 04:05 PM · iosshaking

Shaking GameObject, moving on x-axis in endless runner style game

Hi all,

I've got an issue with a character in an Endless Runner style game, from a tutorial I found on YouTube. (Tutorial here: https://www.youtube.com/playlist?list=PLLH3mUGkfFCXQcNBz_FZDpqJfQlupTznd )


The player 'jitters' (see video) when it's not moved into the correct lane absolutely, it's immediately clear on iOS but I have noticed it occur on Android.

I've updated the Player Position into UI text objects to show the desired lane move changes, (x,y and z respectively).

The player (with a character controller attached) shakes when the player moves and doesn't reach the perfect x-axis values. -2.5, 0 or 2.5 for left, middle or right lanes.

Video (filmed on an iPhone 6, apologies for quality.) Note the X values changing rapidly when the player is shaking.

https://www.youtube.com/watch?time_continue=33&v=jrcnZTAF-CI


The jitter does stop and it often helped by moving along the Y-axis ( Jumping / Sliding in the context of the game).

Has anyone else experienced t$$anonymous$$s issue? Found an elegant solution for it? I've taken a look at the following issues, but not$$anonymous$$ng I've tried has worked. https://forum.unity.com/threads/how-to-smooth-character-movement-that-is-jittery.322417/ https://forum.unity.com/threads/charactercontroller-movement-jittery.40196/


I've seen that some people experience jitters far from the origin, but t$$anonymous$$s occurs close to Zero, so I've discounted that idea. I've tried updating the Updates to LateUpdate / FixedUpdate. I have changed the "Min Move Distance" on the Character Controller to no avail.

I t$$anonymous$$nk the key is fixing the issue is either reac$$anonymous$$ng the desired lane value with a different type of Move on the Character Controller or giving it a margin or error, any ideas on how to ac$$anonymous$$eve t$$anonymous$$s would be great.

I've also attached my code for the playerMotor below.

Unity Version: 2019.1.0f2

PlayerMotor.cs:


     using UnityEngine;
     using UnityEngine.UI;
      
     public class PlayerMotor : MonoBehaviour
     {
         // PARAMETERS
         private const float LANE_DISTANCE = 2.5f;
         private const float TURN_SPEED = 0.05f;
      
         // BOOL
         private bool isRunning = false;
      
         // ANIMATION
         private Animator anim;
      
         // MOVEMENT
         private CharacterController controller;
         private float jumpForce = 4.0f;
         private float gravity = 12.0f;
         private float verticalVelocity;
      
         // SPEED
         private float originalSpeed = 7.0f;
         private float speed;
         private float speedIncreaseLastTick;
         private float speedIncreaseTime = 2.5f;
         private float speedIncreaseAmount = 0.1f;
      
         // DEBUG
         public Text xpos, ypos, zpos;
      
         // LANE FOR PLAYER
         private int desiredLane = 1; // 0 = Left, 1 = Middle, 2 = Right
      
         private void Start()
         {
             speed = originalSpeed;
             controller = GetComponent<CharacterController>();
             anim = GetComponent<Animator>();
         }
      
         private void Update()
         {
             // CHECK FOR BOOLEAN IN GAME MANAGER
             if (!isRunning)
                 return;
      
             if(Time.time - speedIncreaseLastTick > speedIncreaseTime){
                 speedIncreaseLastTick = Time.time;
                 speed += speedIncreaseAmount;
                 // CHANGE THE MODIFIER TEXT
                 GameManager.Instance.UpdateModifier(speed - originalSpeed);
             }
      
             if (MobileInput.Instance.SwipeLeft)
             {
                 MoveLane(false);
             }
      
             if (MobileInput.Instance.SwipeRight)
             {
                 MoveLane(true);
             }
      
             // Calculate where we should be next
             Vector3 targetPosition = transform.position.z * Vector3.forward;
      
             // SWITCH LANES
             if (desiredLane == 0)
             {
                 targetPosition += Vector3.left * LANE_DISTANCE;
                 //Debug.Log(targetPosition);
             }
             else if (desiredLane == 2)
             {
                 targetPosition += Vector3.right * LANE_DISTANCE;
                 //Debug.Log(targetPosition);
             }
      
             // THIS DIDN'T FIX IT
             // ===============================
             //switch(desiredLane){
             //    case 0:
             //        targetPosition = Vector3.left * LANE_DISTANCE;
             //        targetPosition = new Vector3(targetPosition.x, targetPosition.y, transform.position.z);
             //        break;
             //    case 1:
             //        targetPosition = Vector3.zero;
             //        targetPosition = new Vector3(targetPosition.x, targetPosition.y, transform.position.z);
             //        break;
             //    case 2:
             //        targetPosition = Vector3.right * LANE_DISTANCE;
             //        targetPosition = new Vector3(targetPosition.x, targetPosition.y, transform.position.z);
             //        break;
             //}
      
             // CALCULATE THE MOVE DELTA
             Vector3 moveVector = Vector3.zero;
      
             moveVector.x = (targetPosition - transform.position).normalized.x * speed;
      
             // DEVELOPMENT
             Vector3 playerPosition = t$$anonymous$$s.transform.position;
             xpos.text = playerPosition.x.ToString();
             ypos.text = playerPosition.y.ToString();
             zpos.text = playerPosition.z.ToString();
      
             // CALL THE FUNCTION HERE AND ASSIGN TO VARIABLE SO WE'RE NOT CALCULATING TWICE
             bool isGrounded = IsGrounded();
             anim.SetBool("Grounded", isGrounded);
                
             // CALCULATE GRAVITY
             // IS CHARACTER ON THE GROUND
             if (isGrounded)
             {
                 verticalVelocity = -0.1f;
                 // IF PLAYER JUMPS
                 //if (Input.GetKeyDown(KeyCode.Space) || MobileInput.Instance.SwipeUp)
                 if (MobileInput.Instance.SwipeUp)
                 {
                     anim.SetTrigger("Jump");
                     verticalVelocity = jumpForce;
                 } else if (MobileInput.Instance.SwipeDown){
                     // SLIDE
                     StartSliding();
                     Invoke("StopSliding", 1.0f);
                 }
             }
             else
             {
                 // ALWAYS PULL THE CHARACTER DOWN
                 verticalVelocity -= (gravity * Time.deltaTime);
      
                 // FAST FALLING
                 //if (Input.GetKeyDown(KeyCode.Space) || MobileInput.Instance.SwipeDown)
                 if (MobileInput.Instance.SwipeDown)
                 {
                     verticalVelocity = -jumpForce;
                 }
             }
      
             moveVector.y = verticalVelocity;
      
             moveVector.z = speed;
      
             // USE THE MOVE DELTA AND APPLY TO CONTROLLER
             // NEED TO MOVE THE PLAYER TO MOVE VECTOR WITHIN MARGIN OF ERROR?!
             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 MoveLane(bool goingRight)
         {
             // DESIRED LANE TO MOVE TO
             desiredLane += (goingRight) ? 1 : -1;
             desiredLane = Mathf.Clamp(desiredLane, 0, 2);
         }
      
         private bool IsGrounded()
         {
             Ray groundRay = new Ray(new Vector3(controller.bounds.center.x,
                                                 (controller.bounds.center.y - controller.bounds.extents.y) + 0.2f,
                                                 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 StartSliding(){
             anim.SetBool("Sliding", true);
             controller.height /= 2;
             controller.center = new Vector3(controller.center.x, controller.center.y / 2, controller.center.z);
         }
      
         private void StopSliding(){
             anim.SetBool("Sliding", false);
             controller.height *= 2;
             controller.center = new Vector3(controller.center.x, controller.center.y * 2, controller.center.z);
         }
      
         private void OnControllerColliderHit(ControllerColliderHit $$anonymous$$t)
         {
             switch($$anonymous$$t.gameObject.tag){
                 case "Obstacle":
                     Crash();
                 break;
             }
         }
      
         private void Crash(){
             // PLAYER DIES
             anim.SetTrigger("Death");
             isRunning = false;
             GameManager.Instance.OnDeath();
         }
      
     }
 



Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by TomGa83 · Oct 21, 2021 at 09:49 PM

Did you find a solution for t$$anonymous$$s?

Comment

People who like this

0 Show 0 · 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

Answer by mat_capedkoala · Oct 22, 2021 at 11:23 AM

Hi @TomGa83 - I'm afraid I had to rewrite a lot of t$$anonymous$$s project - once the X values became large enough t$$anonymous$$s issue persisted. If you t$$anonymous$$nk of somet$$anonymous$$ng like Temple Run - you're always turning and going back on yourself (so the values remain small). Changing my Update to LateUpdate helped too - but ultimately I couldn't avoid the issue and so had to rewrite. Sorry I couldn't be more help.

Comment

People who like this

0 Show 0 · 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.

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

214 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

Related Questions

camera wont stop stuttering 0 Answers

Unity 5, MP3 hardware decoding on IOS 1 Answer

Weird Bug with iOS Compilation on 5.2.1p3! 0 Answers

Cant find any Exit on Suspend in Unity 5.2 2 Answers

Recording audio and video to stock it into a local file 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