Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by SuperRaed · Aug 23, 2015 at 08:34 PM · physicsrigidbodytriggervector3velocity

getComponent().Velocity always returns 0?

I want to detect the velocity of an object(which is controlled by mouse position ) at the time of entering a trigger . what I've done so far is

void OnTriggerEnter(Collider other){ someVector3 = getComponent().velocity; } but it always evaluates to zero, why is this happening and how can I fix it?

Comment
Add comment · Show 2
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 Positive7 · Aug 23, 2015 at 08:39 PM 0
Share

Did you mean other.GetComponent<Rigidbody>().velocity ?

avatar image SuperRaed · Aug 23, 2015 at 08:54 PM 0
Share

yes, I got the syntax right

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by unity_PNVpFX7xnCpieQ · Mar 24, 2018 at 08:03 AM

Super old, but anyhow since it doesn't have an answer and I needed one, I put a bunch of other semi-related posts together to define a work around and thought I would share since this zero velocity thing made me pull my hair out.

Basically, moving something (like your weapon) with a controller (or motion controller in my case) does not give the rigidbody anything to work with cause there is no force applied to it. It just magically "moves" from one place to the other based on your physical movements. Thus you have to calculate the velocity yourself. It's unfortunately really simple, but took me a while to put it all together.

on the object you want to track Velocity, you need to setup the following variables (note that I did this for 3d but you can do it in 2d as well).

 Vector3 PrevPos; 
 Vector3 NewPos; 
 Vector3 ObjVelocity;

And I initialized them

 void Start()
 {
        PrevPos = transform.position;
        NewPos = transform.position;
 }

Then in my FixedUpdate I calculate the velocity for this GameObject. I used FixedUpdate cause determining velocity over 24 frames/second was good enough and saved resources compared to 120 frames/second using Update.

 void FixedUpdate()
 {
         NewPos = transform.position;  // each frame track the new position
         ObjVelocity = (NewPos - PrevPos) / Time.fixedDeltaTime;  // velocity = dist/time
         PrevPos = NewPos;  // update position for next frame calculation
  }

now in my code I can use ObjVelocity anyway I want. In this case, my controller is hitting a ball, so we send it flying based on the position of my controller and the magnitude of the controller velocity.

 void OnTriggerEnter(Collider other)
 {
         if (other.gameObject.CompareTag("ball"))
         {
             HitBall (other.gameObject, transform, ObjVelocity.magnitude);    
         }
 }


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 Nathanadian · Jan 17, 2021 at 08:53 PM 0
Share

I'd been pulling my hair out for the last two days over this. Thank you!! This worked for me.

avatar image
1

Answer by SuperRaed · Aug 24, 2015 at 08:27 AM

the reason why velocity always returned as 0 was because I never accessed the rigidbody component i.e add force to it, all I did was simply change the object's location based on mouse input. however I want to know how can I combine between taking mouse position with adding force to object?

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

28 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

Related Questions

Rigidbody.velocity Movement code produce wildly different gravity interactions 0 Answers

How to set velocity from connected Rigidbody? 0 Answers

,Two Similar Rigidbodies with velocity behaving differently 1 Answer

A trigger that changes the downforce of any rigidbody that hits it? 0 Answers

Falling Platform 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