Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Coreyf716 · Nov 18, 2012 at 09:06 PM · javascriptfunctionsrigidbodiescalls

Functions and Rigibodies

Modern games lag terribly. I'm aiming for a great performance. Update is called every frame and FixedUpdate is called every fixed frame. Should rigidbody transforms be changed inside of functions such as this one?

 private var hit : RaycastHit;
 private var forward : Vector3;
 function Update() {
      if (Input.GetButtonDown("Fire1"))
           RayFire();
 }
 
 function RayFire() {
      forward = transform.TransformDirection(Vector3.forward);
      if (Physics.Raycast(transform.position, forward, hit, range)) {
           rigibody.AddForceAtPosition(forward * force, hit.point);
      }
 }
Comment
Add comment · Show 6
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 whydoidoit · Nov 19, 2012 at 11:39 AM 0
Share

Yes that's just fine. You can add a force anywhere, you should only modify its position or rotation directly inside FixedUpdate.

avatar image whydoidoit · Nov 19, 2012 at 11:39 AM 0
Share

Apart from you haven't defined forward, hit and range - but I'm guessing this is just example code?

avatar image Coreyf716 · Nov 19, 2012 at 06:28 PM 0
Share

I updated the code with the definitions. Sorry :)

avatar image Coreyf716 · Nov 19, 2012 at 06:29 PM 0
Share

Adding force is modifying its position isn't it?

avatar image whydoidoit · Nov 19, 2012 at 06:32 PM 1
Share

No - it's adding force - I mean really modify the .position / .localPosition

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dukearena · Nov 19, 2012 at 11:28 AM

Hi Corey All rigidibodies functions should be called from FixedUpdate. The code you wrote is right, but try to rename Update with FixedUpdate like this example:

 function FixedUpdate() {
      if (Input.GetButtonDown("Fire1"))
           Fire();
 }
 
 function Fire() {
      if (Physics.Raycast (transform.position, forward, hit, range)) {
           rigibody.AddForceAtPosition(forward * force, hit.point);
      }
 }



Remember that FixedUpdate doesn't use Time.deltaTime but Time.fixedDeltaTime

Comment
Add comment · Show 4 · 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 whydoidoit · Nov 19, 2012 at 11:38 AM 0
Share

That is totally wrong!!! You can't use Input in FixedUpdate all sorts of weird things happen (you miss up and down presses and movements). You can add a force to a rigidbody anywhere - it's just direct modification of position and rotation you have to avoid outside FixedUpdate.

avatar image dukearena · Nov 19, 2012 at 01:43 PM 0
Share

I'm sorry, this thing of input inside FixedUpdate is totally new for me.. Thanks for the news :D

avatar image whydoidoit · Nov 19, 2012 at 01:46 PM 0
Share

Yeah - the problem it is that it "kinda" works! The problem is fixed update may be called many times per frame of your game, or never - depending on whether it is catching up with the game or the game is running faster than it!

Input is updated based on real frames so the frame in which a button is pressed may be processed multiple times by FixedUpdate or never. Likewise everything else.

It's a real gotcha.

avatar image Coreyf716 · Nov 19, 2012 at 06:31 PM 0
Share

I have code to change than. So would my original code work?

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to pass function as parameter to function? 1 Answer

assigned function declaration in unity javascript for different types 1 Answer

Creating & accessing a function from a diffrent "OnTriggerEnter" Function 1 Answer

calling a function from another script 2 Answers

What is the difference between InvokeRepeating & Invoke and how can they be used? 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