- Home /
update and fixed update same script
Hi, is this correct usage of Update and FixedUpdate in same script? Because I use AddForce and Input feature on my game. Some times input does not work in fixed update so I pushed all input functionalities to Update function.
Here is my script.
using UnityEngine;
using System.Collections;
public class BallView : GameView {
void Start(){
app.controller.ballController.OnBallReadyToStart ();
}
void FixedUpdate (){
if (app.model.ballModel.ballRigidBody.velocity.z < app.model.ballModel.velocityLimit) {
Debug.Log(app.model.ballModel.ballRigidBody.velocity.z);
app.model.ballModel.ballRigidBody.AddForce (transform.forward * app.model.ballModel.speed);
}
#if UNITY_STANDALONE_WIN
app.model.ballModel.ballTransform.Translate(Input.GetAxis ("Horizontal"),0,0);
#endif
}
void Update(){
if (Input.GetKeyDown (KeyCode.Space) && app.model.ballModel.isGround == false) {
app.model.ballModel.ballRigidBody.AddForce (Vector3.up * app.model.ballModel.pushForceUp);
app.model.ballModel.isGround = true;
}
}
//Triggers when Ball collide to enemy or ground
//If the ball collide enemy, game will over
void OnCollisionEnter(Collision collision){
app.controller.ballController.OnBallHitsEnemy (collision);
}
}
Answer by jakhir1 · Aug 30, 2015 at 08:40 PM
I don't see anything wrong with that. All your physics calculations should be done in Fixed update and all the Inputs should be in update.
Ok, I am not sure both update functions using in same script. Does this cost on performance?
Your answer
Follow this Question
Related Questions
Interpolation with jumping - Acting Weird. 1 Answer
FixedUpdate limits: consistant 0.01 s on mobile devices? 1 Answer
FixedUpdate performance doubts 2 Answers
Placing Input.GetkeyDown() inside FixedUpdate() slows down the rate I can fire at 1 Answer
Solving Index Out of Bounds on Input.GetTouch(0) in an if statement 3 Answers