- Home /
Question by
aqsanadeem82 · Apr 05, 2019 at 07:46 PM ·
c#unity 5character controllerthird person controllerjittering
Extreme Jittering of character having character controller in unity
HI i have made an Endless Runner game in unity , but the problem is the character is extremely jittery i have tried many ways to fix it but nothing worked for me i have also tried to tweak the values in character controller especially the skin width but nothing helped , you can see the problem in the video here text
The code used is also attached.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMotor : MonoBehaviour {
public AudioSource JumpAudio;
public AudioSource SlideAudio;
public AudioSource DieAudio;
private const float LANE_DISTANCE = 2.5f;
private const float TURN_SPEED=0.05f;
//
private bool isRunning = false;
//Animation
private 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()
{
Speed = originalSpeed;
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
private void Update()
{
if (!isRunning)
return;
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
// anim.SetTrigger("Jump");
anim.SetBool("Jump", true);
verticalVelocity = jumpForce;
JumpAudio.Play();
Invoke("Stopjump", 1.0f);
}
else if (MobileInput.Instance.SwipeDown)
{
//Slide
StartSliding();
Invoke("StopSliding", 1.0f);
SlideAudio.Play();
}
}
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()
{
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 Stopjump()
{
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);
}
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");
isRunning = false;
GameManager.Instance.OnDeath();
DieAudio.Play();
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
switch (hit.gameObject.tag)
{
case "Obstacle":
Crash();
break;
}
}
}
Comment