- Home /
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;
}
}
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.
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.
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
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
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).
This was after slightly increasing the height and zeroing out the y axis so I'm unsure if they are related.
Your answer
Follow this Question
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