- Home /
Fixed Update Problems C# - Please help!
--- I Previously Asked A Question About This ---
have my code for my player movement in script and i have been told to have the physics stuff in the update so that when i pause the game, the physics stuff pauses aswell*
Here is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Movement : MonoBehaviour {
private PauseMenuScript pauseMenuScript;
private Fade myFadeScript;
public float movementSpeed;
public int jumpHeight;
float jumpSpeed;
Vector3 velocity;
void Start(){
pauseMenuScript = GameObject.Find ("Manager").GetComponent<PauseMenuScript> ();
jumpSpeed = Mathf.Sqrt(-2*Physics.gravity.y*jumpHeight) + 0.1f;
}
void FixedUpdate(){
rigidbody.AddRelativeForce(Vector3.right * 10 - rigidbody.velocity);
if(Input.GetKeyDown("up") || (Input.GetMouseButtonDown(0)))
{
velocity = rigidbody.velocity;
velocity.y = jumpSpeed;
rigidbody.velocity = velocity;
}
//if (pauseMenuScript.isPaused)
}
IEnumerator WaitForTime (float waitTime){
yield return new WaitForSeconds (waitTime);
}
}
When the game plays the key press does not always register so the game kinda feels bad, so how would i have it so that this dose not happen in the fixed update.
OR
How could i have it so that in the Update() function, i can reset the veolcity after the game has unpaused
Here is the first thread i posted about this to give you more context:
http://answers.unity3d.com/questions/900398/my-player-fly-across-the-screen-after-unpause-c.html
Thanks :D
Answer by Kumo-Kairo · Feb 14, 2015 at 02:12 PM
You should not check for user input in the FixedUpdate
as it's very often skips the player's input (it may not be called in the input frame). All input should be done in the Update
function.
You should change rigidbody
velocity in the Update
function and continue to AddRelativeForce
in the FixedUpdate
function.