- Home /
[SOLVED] Character Controller moving at different Framrates?
Hello Everybody,
i have a problem: my Character controller is moving as it should at 1000FPS but at 60 it jump too height, the camera head bob is not working and he is walking too slow. I already searched in Unity answers, but i dont found a solution. At my cc.Move(move * Time.deltaTime) i alread implemented delta time and it should be the same at all frames but it is not! This is a very serious problem for my game. I know i could do all in FixedUpdate and the Input in Update but it will move jittering in Fixed Update! Please give me a tip to fix that :(
Best Regards, and Thanks in advance!!
T
Heres my Code:
public CharacterController cc;
public float walkSpeed;
public float runSpeed;
public float crouchSpeed;
public float gravity;
public float jumpSpeed;
public bool canControl;
//health
public float health;
public float Energy;
public float energyIncrease;
public float canSurviveFall;
public float fallDamage;
public float energyIncreaseTime;
public float jumpEnergyCost = 200f;
public float sprintEnergyCost = 1f;
//HUD
public Image healthBar;
public Image energyBar;
//private variables
private float X;
[HideInInspector]
public float remainingHealth;
[HideInInspector]
public float remainingEnergy;
private bool canRun;
private bool canJump;
private float Z;
private float falledDistance;
private Vector3 lastPos;
public Vector3 move = Vector3.zero;
public int movementState = 0; //0 standing , 1 walking , 2 running , 3 air , 4 crouching
void Start()
{
remainingHealth = health;
remainingEnergy = Energy;
canJump = true;
}
void Update ()
{
if (cc.isGrounded && canControl)
{
move = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
move = transform.TransformDirection(move);
move *= walkSpeed;
movementState = 1;
remainingEnergy += energyIncrease;
//if the player is walking or crouching it does not cost any energy
if (Input.GetButton("Sprint") && canRun && remainingEnergy > 1)
{
move *= runSpeed;
remainingEnergy -= sprintEnergyCost;
movementState = 2;
}
if ( Input.GetButtonDown("Jump") && canJump && remainingEnergy > 300)
{
move.y += jumpSpeed;
remainingEnergy -= jumpEnergyCost;
movementState = 3;
}
if (Input.GetButton("Crouch"))
{
cc.height = 1;
move /= crouchSpeed;
movementState = 4;
canRun = false;
}
else
{
cc.height = 2;
canRun = true;
}
}
healthBar.fillAmount = remainingHealth/ 100;
energyBar.fillAmount = remainingEnergy / 1000;
move.y -= gravity;
cc.Move(move * Time.deltaTime);
if (remainingHealth < 1)
{
remainingHealth = 0;
Die();
}
if (remainingEnergy < 0)
{
remainingEnergy = 0;
}
if (remainingEnergy > Energy)
remainingEnergy = Energy;
if (!cc.isGrounded)
{
movementState = 3;
}
}
void Die()
{
Destroy(gameObject);
}
One problem is, that when you jump, you do one time "move.y += jumpSpeed;" but many times "move.y -= gravity;". So at 1000 FPS you have much more gravity as if you have 50 FPS.
Hi,
thank you for your answer. But i cant just run move.y -= jumpSpeed at void Start(). Do you think i should put the gravity into FixedUpdate. The problem is that in fixed update player movement fells jittering. I already tryiend move.y -= gravity * Time.deltaTime but it does nothing. It does not makes the gravity frameindependent. Why is that so difficult :(
Regards,
T
Answer by K_Tec · Aug 03, 2017 at 11:31 AM
Hello everybody,
i sloved it!! I put all my Code in Fixed Update (even the input) and decreased my fixed timestep to 0.006 Withpout vsync i have now 1100 instaed of 1250 fps thats okay! I only let the code with the health bar in the update method.
Thanks everybody!!!
Answer by Bieere · Aug 02, 2017 at 06:21 PM
If you've already implemented the Time.delta time, you could try setting your rigidbody to Interpolate using interpolate shown below.
It would be helpful to see your CharacterController script as well.
Hi,
thank you for the answer, but this is my Character Controller script. Im moving it in Line 77. $$anonymous$$y Character only has a rigidbody wich is kinematic. But i dont thinkt hat setting it to interpolate will fix that :)
Answer by Timo326 · Aug 02, 2017 at 08:40 PM
Try to use a Force for jumping. Maybe you can use a target framerate and optimate it on this when you don't need 1000 FPS.
Sure i could but its a multiplayer game and if you have 50 or 120 fps makes a big difference. Is there really no way? It hsould work because at cc.move() i used *time.deltaTime, but its still not fra$$anonymous$$depending. Its moving faster on higher fps.
This is amking me mad Please help :((
Answer by a161803398874 · Aug 02, 2017 at 09:07 PM
time.FixedDeltaTime maybe can help!
in case it doesent ill check the code for now just change it this function it independent of the frame rate