How to calculate a GameObject's speed in Unity?How to use Git for Unity3D source control?How to prepare a Unity project for git?Changing character speed in UnityUnity - Overriding GameObject's position attributeUnity plays only a few gameobject's soundAndroid touch camera control with inertia in Unity3dUnity Setting WrapMode of List Of GameObject's TexturesUnity c# gameobject's overrideUnity 3D Boxcast Not Registering ObjectTranslated grapple physics from Processing to Unity to get different results
Should my PhD thesis be submitted under my legal name?
What would be the benefits of having both a state and local currencies?
The plural of 'stomach"
Have I saved too much for retirement so far?
Can I use my Chinese passport to enter China after I acquired another citizenship?
Hostile work environment after whistle-blowing on coworker and our boss. What do I do?
The baby cries all morning
How was Earth single-handedly capable of creating 3 of the 4 gods of chaos?
Lay out the Carpet
Why Were Madagascar and New Zealand Discovered So Late?
Tiptoe or tiphoof? Adjusting words to better fit fantasy races
Is there a problem with hiding "forgot password" until it's needed?
What is the oldest known work of fiction?
How to be diplomatic in refusing to write code that breaches the privacy of our users
Is there a good way to store credentials outside of a password manager?
Go Pregnant or Go Home
Why does John Bercow say “unlock” after reading out the results of a vote?
Student evaluations of teaching assistants
Why did Kant, Hegel, and Adorno leave some words and phrases in the Greek alphabet?
Opposite of a diet
Increase performance creating Mandelbrot set in python
How can I get through very long and very dry, but also very useful technical documents when learning a new tool?
Transcription Beats per minute
Trouble understanding overseas colleagues
How to calculate a GameObject's speed in Unity?
How to use Git for Unity3D source control?How to prepare a Unity project for git?Changing character speed in UnityUnity - Overriding GameObject's position attributeUnity plays only a few gameobject's soundAndroid touch camera control with inertia in Unity3dUnity Setting WrapMode of List Of GameObject's TexturesUnity c# gameobject's overrideUnity 3D Boxcast Not Registering ObjectTranslated grapple physics from Processing to Unity to get different results
I have tried something like
float speed = (currentPosition - previousPosition).magnitude / Time.deltaTime
in Update()
.
But I get very bad results because currentPosition
and previousPosition
are too close. After subtraction there's too much rounding error. The speed values fluctuate too much.
And also I don't want to average across multiple frames because that produces delays and only improves the result a little.
Is there any better way to calculate the speed of an object?
unity3d
add a comment |
I have tried something like
float speed = (currentPosition - previousPosition).magnitude / Time.deltaTime
in Update()
.
But I get very bad results because currentPosition
and previousPosition
are too close. After subtraction there's too much rounding error. The speed values fluctuate too much.
And also I don't want to average across multiple frames because that produces delays and only improves the result a little.
Is there any better way to calculate the speed of an object?
unity3d
1
Calculate per second and update with linearly interpolates.
– shingo
Mar 7 at 11:57
add a comment |
I have tried something like
float speed = (currentPosition - previousPosition).magnitude / Time.deltaTime
in Update()
.
But I get very bad results because currentPosition
and previousPosition
are too close. After subtraction there's too much rounding error. The speed values fluctuate too much.
And also I don't want to average across multiple frames because that produces delays and only improves the result a little.
Is there any better way to calculate the speed of an object?
unity3d
I have tried something like
float speed = (currentPosition - previousPosition).magnitude / Time.deltaTime
in Update()
.
But I get very bad results because currentPosition
and previousPosition
are too close. After subtraction there's too much rounding error. The speed values fluctuate too much.
And also I don't want to average across multiple frames because that produces delays and only improves the result a little.
Is there any better way to calculate the speed of an object?
unity3d
unity3d
asked Mar 7 at 11:44
landingslandings
847
847
1
Calculate per second and update with linearly interpolates.
– shingo
Mar 7 at 11:57
add a comment |
1
Calculate per second and update with linearly interpolates.
– shingo
Mar 7 at 11:57
1
1
Calculate per second and update with linearly interpolates.
– shingo
Mar 7 at 11:57
Calculate per second and update with linearly interpolates.
– shingo
Mar 7 at 11:57
add a comment |
1 Answer
1
active
oldest
votes
You could use a coroutine that calculates the speed slower than every frame:
void OnEnabled()
StartCoroutine(SpeedReckoner());
public float Speed;
public float UpdateDelay;
private IEnumerator SpeedReckoner()
YieldInstruction timedWait = new WaitForSeconds(UpdateDelay);
Vector3 lastPosition = transform.position;
float lastTimestamp = Time.time;
while (enabled)
yield return timedWait;
var deltaPosition = (transform.position - lastPosition).magnitude;
var deltaTime = Time.time - lastTimestamp;
if (Mathf.Approximately(deltaPosition, 0f)) // Clean up "near-zero" displacement
deltaPosition = 0f;
Speed = deltaPosition / deltaTime;
lastPosition = transform.position;
lastTimestamp = Time.time;
Your answer is very helpful. Thank you.
– landings
Mar 8 at 5:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55042997%2fhow-to-calculate-a-gameobjects-speed-in-unity%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use a coroutine that calculates the speed slower than every frame:
void OnEnabled()
StartCoroutine(SpeedReckoner());
public float Speed;
public float UpdateDelay;
private IEnumerator SpeedReckoner()
YieldInstruction timedWait = new WaitForSeconds(UpdateDelay);
Vector3 lastPosition = transform.position;
float lastTimestamp = Time.time;
while (enabled)
yield return timedWait;
var deltaPosition = (transform.position - lastPosition).magnitude;
var deltaTime = Time.time - lastTimestamp;
if (Mathf.Approximately(deltaPosition, 0f)) // Clean up "near-zero" displacement
deltaPosition = 0f;
Speed = deltaPosition / deltaTime;
lastPosition = transform.position;
lastTimestamp = Time.time;
Your answer is very helpful. Thank you.
– landings
Mar 8 at 5:40
add a comment |
You could use a coroutine that calculates the speed slower than every frame:
void OnEnabled()
StartCoroutine(SpeedReckoner());
public float Speed;
public float UpdateDelay;
private IEnumerator SpeedReckoner()
YieldInstruction timedWait = new WaitForSeconds(UpdateDelay);
Vector3 lastPosition = transform.position;
float lastTimestamp = Time.time;
while (enabled)
yield return timedWait;
var deltaPosition = (transform.position - lastPosition).magnitude;
var deltaTime = Time.time - lastTimestamp;
if (Mathf.Approximately(deltaPosition, 0f)) // Clean up "near-zero" displacement
deltaPosition = 0f;
Speed = deltaPosition / deltaTime;
lastPosition = transform.position;
lastTimestamp = Time.time;
Your answer is very helpful. Thank you.
– landings
Mar 8 at 5:40
add a comment |
You could use a coroutine that calculates the speed slower than every frame:
void OnEnabled()
StartCoroutine(SpeedReckoner());
public float Speed;
public float UpdateDelay;
private IEnumerator SpeedReckoner()
YieldInstruction timedWait = new WaitForSeconds(UpdateDelay);
Vector3 lastPosition = transform.position;
float lastTimestamp = Time.time;
while (enabled)
yield return timedWait;
var deltaPosition = (transform.position - lastPosition).magnitude;
var deltaTime = Time.time - lastTimestamp;
if (Mathf.Approximately(deltaPosition, 0f)) // Clean up "near-zero" displacement
deltaPosition = 0f;
Speed = deltaPosition / deltaTime;
lastPosition = transform.position;
lastTimestamp = Time.time;
You could use a coroutine that calculates the speed slower than every frame:
void OnEnabled()
StartCoroutine(SpeedReckoner());
public float Speed;
public float UpdateDelay;
private IEnumerator SpeedReckoner()
YieldInstruction timedWait = new WaitForSeconds(UpdateDelay);
Vector3 lastPosition = transform.position;
float lastTimestamp = Time.time;
while (enabled)
yield return timedWait;
var deltaPosition = (transform.position - lastPosition).magnitude;
var deltaTime = Time.time - lastTimestamp;
if (Mathf.Approximately(deltaPosition, 0f)) // Clean up "near-zero" displacement
deltaPosition = 0f;
Speed = deltaPosition / deltaTime;
lastPosition = transform.position;
lastTimestamp = Time.time;
answered Mar 7 at 12:45
ImmersiveImmersive
84049
84049
Your answer is very helpful. Thank you.
– landings
Mar 8 at 5:40
add a comment |
Your answer is very helpful. Thank you.
– landings
Mar 8 at 5:40
Your answer is very helpful. Thank you.
– landings
Mar 8 at 5:40
Your answer is very helpful. Thank you.
– landings
Mar 8 at 5:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55042997%2fhow-to-calculate-a-gameobjects-speed-in-unity%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Calculate per second and update with linearly interpolates.
– shingo
Mar 7 at 11:57