Question by
demongoatgamer · Jun 25, 2020 at 03:02 PM ·
controllerframeratetime.deltatime
Game runs faster at higher framerates
As far as I know, I have used Time.deltaTime correctly as to negate speed differences at different framerates, but my player and camera still moves much faster at lower graphics settings, most likely because of the framerate. Does anyone have any idea what I've done wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
private float crouchMultiplier = 1f;
private Vector3 velocity;
private bool isGrounded;
private bool isCrouched;
private float velocityDecay = 1f;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * crouchMultiplier * velocityDecay * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
isCrouched = true;
gameObject.transform.localScale = new Vector3(1, 1f, 1);
}
if (Input.GetKeyUp(KeyCode.LeftControl))
{
crouchMultiplier = 1f;
gameObject.transform.localScale = new Vector3(1, 2f, 1);
velocityDecay = 1;
isCrouched = false;
}
if (isCrouched)
{
if (velocityDecay > 0.2f && isGrounded)
{
crouchMultiplier = 2.5f;
velocityDecay *= 0.995f;
}
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to get 60 Frames per second in iOS? 0 Answers
deltaTime on fixedUpdate 1 Answer
Unity 3D rigidbody collision doesnt work correctly 0 Answers
Physics based Third Person Controller 0 Answers