- Home /
FixedUpdate inside an Input Event
Hey, just a quick question. I want to know how to put a FixedUpdate inside an Input.GetMouseButtonUp event.
I want to move the main camera when I've clicked on a game object in C#
The following code is how the camera moves :
void FixedUpdate() {
float newPositiony = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);
transform.position = new Vector3(transform.position.x, newPositiony, transform.position.z);
Answer by Julien-Lynge · Aug 23, 2011 at 05:21 PM
You've got your wires a bit crossed here, Martin. FixedUpdate is an function that is called every time Unity updates the physics in your scene. Input.GetMouseButtonUp is an attribute that you can read to see if the mouse button was released this frame. Talking about putting a function within an attribute is meaningless.
What you probably want to do is:
void Update() //this is called every frame
{
if (Input.GetMouseButtonUp(0)) //the left mouse button was released
{
//do something
}
}
As @NOAA_Julien said, you can't put FixedUpdate() inside another function. Usually camera movement is done in Update, not FixedUpdate - you may have some jitter at certain framerates, because Update (which is related to rendering, the camera job) runs at the frame rate, while FixedUpdate (physics related) runs at 50 cycles per second (default speed). Input also is supposed to be used in Update - this Get$$anonymous$$ouseButtonUp function, for instance, will return true only during the update cycle where you released the button. If your framerate is higher than 50, you can loose the event; if it's lower, you can have two or more events in consecutive FixedUpdate cycles.
Oh crap. :S ....Hi Julien & aldonaletto. I've read alright (and seen from what I've been doing so far) that moving the camera inside FixedUpdate is not at all suitable. I was moving that camera inside Input.Get$$anonymous$$ouseButtonUp, but then the camera will move every time the mouse button goes 'Up', but I only want it to move when the mouse button goes up after I've selected a specific game object. I guess the problem is how I've been trying to move the camera in that case? Is there a better way for me to move the camera rather than in the FixedUpdate? Below is the code I've applied to the camera that I'm moving, and the transform I've attached to it is the game Object I want my camera to move in on. Or would ye recommend that I zoom?
Thanks for the great help too! :)
public class $$anonymous$$oveCamera : $$anonymous$$onoBehaviour { public Transform target; public float smoothTime = 3.3F; private float yVelocity = 0.0F; private float xVelocity = 0.0F; private float zVelocity = 0.0F; void FixedUpdate() {
print("Starting " + Time.time);
StartCoroutine(WaitAndPrint(4.0F));
print("Before WaitAndPrint Finishes " + Time.time);
}
IEnumerator WaitAndPrint(float waitTime) {
yield return new WaitForSeconds(waitTime);
print("WaitAndPrint " + Time.time);
float newPositiony = $$anonymous$$athf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);
transform.position = new Vector3(transform.position.x, newPositiony, transform.position.z);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Enemy won't damage player C# 1 Answer