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 MSten · Feb 01, 2014 at 08:26 PM · rigidbodyvelocitylerpmovevector

How can I find the velocity on a rigidbody which moves using Lerp?

Hello,

I need some help with finding the velocity on an object which moves using a Vector.Lerp. It seems that if you move an object the way stated, a velocity on the rigidbody is not applied.

This seems strange to me as the lerp uses a time in which it moves the object between two points.

Is there something simple that i have missed, or is there a way to find the velocity of an object which moves using lerp?

Here is some sample code, never mind some of the bad scripting since it's only a test!

Thanks in advance!

 using UnityEngine;
 using System.Collections;
 
 public class MyPlayerController : MonoBehaviour
 {
     public Transform myTransform;
     public Vector2 currentLocation;
     public Vector2 newLocation;
 
     public float newLocationY;
     public float smooth;
 
     public float nextMove = 0.1f;
     public float moveRate = 0.1f;
 
     public float tilt;
 
     void Start()
     {
         myTransform = transform;
     }
 
     void Update()
     {
         currentLocation = transform.position;
 
         if(Input.GetKeyDown(KeyCode.LeftArrow) && Time.time > nextMove && myTransform.position.y < 8f)
         {
             nextMove = Time.time + moveRate;
             newLocationY = currentLocation.y + 3f;
             newLocation = new Vector2(0, newLocationY);
         }
         else if(Input.GetKeyDown(KeyCode.RightArrow) && Time.time > nextMove && myTransform.position.y > -8f)
         {
             nextMove = Time.time + moveRate;
             newLocationY = currentLocation.y -3f;
             newLocation = new Vector2(0, newLocationY);
         }
         //Move
         myTransform.position = Vector2.Lerp(currentLocation, newLocation, smooth * Time.deltaTime);
         //Rotate
 
                                                         //!
                                                         //!This is always nort!
         myTransform.rotation = Quaternion.Euler(0, 0, rigidbody2D.velocity.y * -tilt);
 
         //This ALWAYS displays (0.0, 0.0)
         Debug.Log(rigidbody2D.velocity);
     }
 }
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

1 Reply

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

Answer by Owen-Reynolds · Feb 01, 2014 at 09:01 PM

Velocity only applies to the built-in physics system to let the ball fall, bounce and roll on it's own. It sort of means "how much is the automatic movement?"

So, if you're moving it yourself, with Lerp or however, velocity should be zero. If it wasn't zero, the ball would always move a little extra "randomly" from where you put it.

Imagine you're letting physics drop a ball (using a rigidbody.) It gains speed, falls, and the Y velocity auto-increases. When it hits and bounces, Y-velocity flips and is reduced (based on how bouncy it is.) OK, now imagine your code 1-time teleports the ball sideways by x=2 in a single frame. That doesn't change velocity at all. The ball keeps falling at the same speed, from the new position, like a RoadRunner cartoon. Or the old dungeon teleporter trap at the bottom of a pit.

You can compute velocity yourself: new position minus old position. In line 41. currentLocation is now where you used to be, so: Vector3 vel = transform.position - currentLocation;. You might think "but that's just how much I moved!" But that's the definition of velocity.

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 MSten · Feb 01, 2014 at 09:49 PM 0
Share

Wow, thanks for the very accurate and well explained answer! The reason I got so confused was that in Unity's "Space Shooter" tutorial, they use about the same setup for moving the player, but without the lerp. But I will take another look at the code with this new information, thanks again!

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

19 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

Related Questions

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

Add a wind vector to local velocity for calculating wind speed 2 Answers

rigidBody velocity acting up 2 Answers

Rigidbody velocity capped at 0.4 1 Answer

How do i add a boost in velocity to a 2D ball when you press it? 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