• 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
Question by REICHTAG · Jan 09, 2018 at 01:07 PM · 2d game2d-physicsspeedtrackingchecking

how to track object movement speed 2d

Hi . In my game this far I have a object that spawns in to the game .I dont apply any move force to it it only have RigitBody 2d on it and box colider . Is there any way to calculate movement speed in space ? I need to calculate move speed to determine impact damage once object has collided with other object. So higher move speed will result in gigher damage number . Only problem is how do I find out speed of that object before it collides with other object ? Thanks

Comment
HosseinArabbeigi

People who like this

1 Show 1
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 TreyH · Jan 09, 2018 at 04:04 PM 0
Share

sisse008 has already posted what should be the correct answer, but in case you weren't sure how to estimate speed manually:


 using UnityEngine;
 using System.Collections;
 
 public class CheckMySpeed : MonoBehaviour
 {
     private Vector3 previousPosition;
     private float estimatedSpeed;
 
     // Take an initial value for our position.  The first value might end up
     // being zero, but this tends to happen with numerical approximations.
     void Awake ()
     {
         this.previousPosition = this.transform.position;
     }
 
     // It sounded like you were using physics on your objects, so we'll
     // check the speed in the physics update
     void FixedUpdate ()
     {
         // "Speed" is defined as the change in position divided by the change in time.
         float delta = (this.transform.position - previousPosition).magnitude;
         this.estimatedSpeed = delta / Time.fixedDeltaTime;
 
         // Confirm it's working
         Debug.LogFormat ("CheckMySpeed.estimatedSpeed: {0}", this.estimatedSpeed);
     }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by sisse008 · Jan 09, 2018 at 01:16 PM

you can get magnitude of velocity:

 float mag = GetComponent<Rigidbody2D>().velocity.magnitude;



Comment
REICHTAG
HosseinArabbeigi

People who like this

2 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 REICHTAG · Jan 09, 2018 at 02:12 PM 0
Share

by this metode volocity is getting bigger and bigger as long as my object is moving even if the speed in game looks the same ! For example if objects free falls long distance straight down - you can see that it falls at the same speed all the time but numbers from float mag = GetComponent().velocity.magnitude; are growing until object stops !So this metod is not working in my case . What I need is to get move speed as it is if object moves faster number should be higher and if it moves slower number should be lower .

avatar image sisse008 REICHTAG · Jan 09, 2018 at 03:31 PM 2
Share

https://docs.unity3d.com/ScriptReference/Collision2D-relativeVelocity.html

avatar image TreyH sisse008 · Jan 09, 2018 at 04:03 PM 0
Share

This should be the answer @REICHTAG unless you have a requirement that the calculation must be prior to collision.

avatar image

Answer by Cambesa · Jan 09, 2018 at 03:26 PM

You could subtract their position Vectors, it will result in a new Vector that indicates the force and direction of impact. Multiply this with Time.deltaTime to see how much force/second it has. It's a framerate independent measurement. Please do keep in mind relativity. An object moving downwards with 100 units/time colliding with an object moving upwards with 100 units/time will result in an impact of 200 units. But when the 2nd object is not moving, aka 0 units/time, it will have an impact of 100 units.

Kind regards, Yorick

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

85 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

want to knock gameobject to the same direction as the projectile 1 Answer

Unity 2D Collider Safe Sizes 0 Answers

How to make my space ship shooting (2D Game)? 0 Answers

2D Trajectory Curve,Display trajectory while dragging 2 Answers

How do i increase Speed while running 2d 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