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 Dan 12 · Nov 11, 2010 at 10:04 PM · physicsvelocitytimeraccelerationinterval

Calculating interval dependants; ie: Velocity

I haven't been saving my scripts but just testing different approaches...

Having established a model of zero gravity flight using AddForce / AddRelative... and AddTorque / AddRelative..., I am trying to calculate velocity... Firstly taking "rigidbody.velocity.magnitude" to get m/s as velocity which reports back to a GUI label.

The problem arises when I want to take sample of say velocity or displacement, then wait one second (or greater, dependant on the granularity of response) and calculate say velocity by averaging the recorded variables?

My methods thus far have been to use the "yield.wait second(x)" but that has to be an "Ienumerator" or "Ienumarable" method else I get thrown a not correct method type class!

I have tried using "System.Timers" namespace and that doesn't seem to work in Unity?

So I am trying to sample say "rigidbody.velocity.magnitude" or -- ultimately-- say the x,y,z magnitudes of a Vector3 [ per second or other arbitary time interval ] to derive a GUI read out of acceleration ( displacement/time etc.)

I'm not asking for complete solution but assistance on the method by which I can say read ( current velocity, joystick input, Vector3 magnitudes, etc) on a periodic basis ( x seconds) and pass that to a GUI Label

TIA

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by StephanK · Nov 12, 2010 at 12:08 AM

Your first idea, using yield, was correct. What you need is a coroutine. You should look those up in the docs. THis should give you a basic idea (c#):

void Start() { StartCoroutine(MyRoutine(3)); }

IEnumerator MyRoutine(float interval) { while (true) { DoSomething(); yield return new WaitForSeconds(interval); } }

This will DoSomething() every 3 seconds.

Comment
Add comment · Show 1 · 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 Dan 12 · Nov 12, 2010 at 12:51 PM 0
Share

I've spent a couple of hours trying to get the startcoroutine() yield WaitFor Seconds working without error but I am stuck with error CS1041"identifier expected" once at 24,43 and at 24,49 also CS0100 ""NeedSomeGeneratorHere" is a duplicate" at 24,43 also. I really have no idea what is wrong here and googling the errors makes me think I am not understanding the iEnumerator method perhaps it needs a data type to be iterating with some extra syntax than normal methods' statements?

I'm now trying to see how to post code without running out of characters does [code] [/code] work here? :sighs

avatar image
0

Answer by Dan 12 · Nov 12, 2010 at 05:07 PM

Phew ... After a lot of head scratching I have it working so I have a template for interval dependant future functions, thanks to spree for the pointer.

using UnityEngine;

using System.Collections;

public class Speedometer_PhysX : MonoBehaviour {

// Use this for initialization void Start () { StartCoroutine (Acceleration(cycletime));

} public float cycletime = 1.0f; public GameObject speedtarget; float accResult; float accSample1; float accSample2; decimal acceleration; decimal speed;

// Update is called once per frame void Update () {

}

IEnumerator Acceleration(float interval) { while(true) { accSample1 = speedtarget.rigidbody.velocity.magnitude; yield return new WaitForSeconds(interval); accSample2 = speedtarget.rigidbody.velocity.magnitude; accResult = SampleAcceleration(accSample1,accSample2); } }

public float SampleAcceleration(float a1, float a2) { return accResult = (a2-a1)/cycletime; }

void OnGUI () { acceleration = (decimal) accResult; speed = (decimal) speedtarget.rigidbody.velocity.magnitude; GUI.Label(new Rect(10,10,2000,25),"Speed = "+speed+ " | Acceleration = "+acceleration+" m/s"); //GUI.Label(new Rect(20,20,2000,25),"new label");

}

}

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Accelerate a rigidbody towards max speed 2 Answers

Simulating Fighter Jet Physics (with addForce () ) 2 Answers

Obtaining Constant Speed with AddForce 1 Answer

Character accelerates while running towards walls. "Sonic effect" 1 Answer

arrows in unity 3d? 0 Answers


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