Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by martin_cork · Aug 23, 2011 at 03:32 PM · c#noobfixedupdate

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);
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
   }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Aug 23, 2011 at 05:35 PM 0
Share

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.

avatar image martin_cork · Aug 23, 2011 at 11:01 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Enemy won't damage player C# 1 Answer

Update and Fixed update function synchronicity 1 Answer

How can I control Spawn speed/rate from an Object spawner? I'm new to Unity and scripting, and would like to delay the spawn rate of objects coming out of an ObjectPooler. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges