Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
9
Question by Bob5602 · Dec 16, 2010 at 02:26 PM · javascriptrigidbodyvelocity

How can I calculate velocity without using Rigidbody?

Hello everyone!

This is a problem I have run into while making my space shooter style game. I have a script that I put onto my "targets" in which I get its rigidbody.velocity. I then use that script on my aiming turrets to create a believable "lead time" effect and actually hit when the turret fires. However, I am running into a problem with my "missiles." In order to get them to fly to the target, more or less, I am using transform.translate. Using that and a speed variable (and time.deltatime), I am able to get my missile to where it needs to go.

Now, I have a rigidbody on the missile for collisions and such, but my script doesn't appear to be getting any information about the speed / direction of the missile. I presume its because I am moving it via a transform translate and not a rigidbody.AddForce, but it really behaves a lot better this way.

Is there a way I can "fake" the speed information I would normally get from rigidbody.velocity on my missile object that is moved by transform.translate?

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

3 Replies

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

Answer by Statement · Dec 16, 2010 at 02:36 PM

Simple & crude velocity measure:

You can sample the world position on this frame and store the result from the previous frame. You now have two points of reference. Take into consideration how long the time slice was between the two points to derive the velocity. I think the code would be:

var velocity = (current - previous) / Time.deltaTime;

If the leap (current - previous) was big, say 5 units to the right and the time slice was small (1/60), we would be going very fast. 5/(1/60) = 300 units per second.

Comment
Add comment · Show 10 · 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 Bob5602 · Dec 16, 2010 at 02:42 PM 0
Share

Hmm yeah, thats what I was thinking. Getting back into the roots of it all :)

That should be doable, with an extra variable to store the last position. So if I put this into an update function, it would look something like

var previous : Vector3; var velocity : Vector3; function Update(){ velocity = (transform.position - previous) / Time.deltaTime; previous = transform.position; }

?

avatar image Statement · Dec 16, 2010 at 02:44 PM 1
Share

Bingo. That's right.

avatar image Bob5602 · Dec 16, 2010 at 02:46 PM 0
Share

Cool thanks. Thats what I had originally thought but wasn't sure if the differences per frame would be significant enough.

avatar image Statement · Dec 16, 2010 at 03:18 PM 2
Share

Erm, it should. Otherwise you can ins$$anonymous$$d multiply with (1.0f / Time.deltaTime). Remember to check Time.deltaTime for 0 since division by zero causes exceptions. This happen if you set your time scale to zero (i.e. pause the game).

avatar image Peter G · Dec 16, 2010 at 03:48 PM 2
Share

Operator / http://unity3d.com/support/documentation/ScriptReference/Vector3-operator_divide.html

Show more comments
avatar image
17

Answer by Celestium · Sep 17, 2013 at 11:33 AM

this works:

 var previous: Vector3;
 var velocity: float;
 
 function Update()
 {
      velocity = ((transform.position - previous).magnitude) / Time.deltaTime;
      previous = transform.position;
 
      print (velocity);
 
 }
Comment
Add comment · Show 3 · 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 LaneFox · Jun 19, 2015 at 01:56 AM 5
Share

Velocity is a direction, speed is a float. This doesn't get Velocity.

avatar image BL1TZ LaneFox · May 14, 2018 at 09:50 AM -1
Share

@LaneFox Yes it does, look at how (Vector3 Transform.position - Vector3 previous).$$anonymous$$AGNITUDE is used. $$anonymous$$agnitude will return the size of the vector, which is in fact a float.

This answer is correct and is a solution.

avatar image Split3 BL1TZ · Jul 31, 2020 at 06:42 PM 2
Share

Velocity is a measure of an object's speed and direction. ... The magnitude of the velocity is by definition identical to the speed, which is a scalar quantity, not a vector, and never negative.

avatar image
1

Answer by nickazg · Jan 10, 2012 at 08:50 AM

Hi I was working on the same thing as this recently, and I managed to get something that works for me, even if the velocity hits zero. I only set this up to work on the Y axis but should be easy easy enough to calculate the other axis too.

var camPos : Transform;

Update () {

StartCoroutine(curVelocity(0.1));

}

function curVelocity(waitTime : float)

{

      var previous : float;
      var current : float;
      var velocity : float;
      previous = camPos.position.y;
      yield WaitForSeconds (waitTime);
      current = camPos.position.y;
      velocity = current - previous;
      if (velocity == 0)
      {
     velocity = 0.000001;
      }
      velocity = velocity / waitTime;
      if (velocity < 0.0001 && velocity > -0.0001)
      {
     velocity = 0;
      }
             
 
      print (velocity);

}

Comment
Add comment · Show 5 · 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 WorldWideGlide · May 19, 2014 at 03:26 PM 0
Share
 velocity = ((transform.position - previous).magnitude) / Time.deltaTime;
              previous = transform.position;
     

   

I've been using this code to calculate the velocity of my transform and it only works intermittently..... $$anonymous$$y resulting velocity will jump very erratically from 0 to it's current value as my transform is moving, I also noticed it tends to become even more erratic when my camera is facing certain directions. Other times it will work just fine for an hour or so.....

avatar image Pawl WorldWideGlide · Mar 03, 2016 at 08:22 PM 2
Share

You may consider lerping the velocity to avoid single frame irregularities.

     public Vector3 FrameVelocity { get; set; }
     public Vector3 PrevPosition { get; set; }
 
 public void Update() {
     // $$anonymous$$eep an average velocity due to fixed update irregularity, else we will occassionally get 0 velocity
     Vector3 currFrameVelocity = (transform.position - PrevPosition) / Time.deltaTime;
     FrameVelocity = Vector3.Lerp(FrameVelocity, currFrameVelocity, 0.1f);
     PrevPosition = transform.position;
 }
avatar image smackledorf Pawl · May 02, 2020 at 12:35 AM 0
Share

people should know this is the solution for measuring velocity on each axes individually, with positive and negative directions. The solutions above use floats ins$$anonymous$$d of vectors and are only magnitude. Thank you Pawl!

avatar image ashwinFEC · Jul 23, 2015 at 10:35 AM 2
Share

@WorldWideGlide try putting that code in LateUpdate()

avatar image satyambhatt20 · May 27 at 01:39 PM 0
Share

alt text Thanks brother works like a charm.

1.png (78.1 kB)

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

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Movement using rigidbody.velocity to apply a constant force until stop 1 Answer

Dragging object out of position 2 Answers

Rigidbody - Applying One-Time Force? Not Constant Force 2 Answers

How to set velocity of Rigidbody without changing gravity? 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