- Home /
Player moves faster when fps is higher?
Hello Unity community, Cobalt 60 here and I was wondering? why does my player move faster when the frames per second... Like when my game is at 80 fps he moves at the perfect speed. but when I hit 160 fps or higher than 80 or lower than 80 the player's top speed is changed. I have seen tutorials where you add some thing like Time.Deltatime I just want to know how to implement this in thy script lol. here it is if you need some reference.
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{
private GameObject _GameManager;
public Vector3 movement;
public float moveSpeed = 6.0f;
public float jumpSpeed = 5.0f;
public float drag = 2;
private bool canJump = true;
void Start()
{
_GameManager = GameObject.Find("_GameManager");
}
void Update ()
{
Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
Vector3 forwardForce = forward * Input.GetAxis("Vertical") * moveSpeed;
rigidbody.AddForce(forwardForce);
Vector3 right= Camera.main.transform.TransformDirection(Vector3.right);
right.y = 0;
right = right.normalized;
Vector3 rightForce= right * Input.GetAxis("Horizontal") * moveSpeed;
rigidbody.AddForce(rightForce);
if (canJump && Input.GetKeyDown(KeyCode.Space))
{
rigidbody.AddForce(Vector3.up * jumpSpeed * 100);
canJump = false;
_GameManager.GetComponent().BallJump();
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Destroy")
{
_GameManager.GetComponent().Death();
Destroy(gameObject);
}
else if (other.tag == "Coin")
{
Destroy(other.gameObject);
_GameManager.GetComponent().FoundCoin();
}
else if (other.tag == "SpeedBooster")
{
movement = new Vector3(0,0,0);
_GameManager.GetComponent().SpeedBooster();
}
else if (other.tag == "JumpBooster")
{
movement = new Vector3(0,0,0);
_GameManager.GetComponent().JumpBooster();
}
else if (other.tag == "Teleporter")
{
movement = new Vector3(0,0,0);
_GameManager.GetComponent().Teleporter();
}
}
void OnCollisionEnter(Collision collision)
{
if (!canJump)
{
canJump = true;
_GameManager.GetComponent().BallHitGround();
}
}
}
I would fix it for you, but since the last update here, UA totally messes up the source code of questions. It would be the best when you yould copy your original code again and replace this mess with the original code, but this time don't forget to select everything that is code and press the "101 010" button before you submit. This will indent the selected text and UA will recognise it as code.
Just press the edit button to edit your question.
I can tell from the question title that you need to use Time.deltaTime. The docs explain it quite well, please read them.
Answer by Fattie · Sep 03, 2012 at 05:37 AM
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.FixedUpdate.html
"FixedUpdate should be used instead of Update when dealing with Rigidbody"
"For example when adding a force to a rigidbody, you have to apply the force every fixed frame inside FixedUpdate instead of every frame inside Update"
Hopefully that helps out
BTW here's how you make simple timers in U when you're a beginner
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Invoke.html