• 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 NerdRageStudios · Jan 22, 2015 at 08:03 PM · transformvector3transform.position

How to determine speed of an object when using transform.position

Hi, I am using the following code to rotate and move the player

         float cameraSmooth = 5.0f;
         float modX;
         float modZ;
         float moveSpeed = 50.0f;
         
         modX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
         modZ = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
         
         modifier.z = modZ;
         modifier.x = modX;
 
         transform.position = Vector3.Lerp(transform.position, transform.position + modifier, Time.deltaTime * cameraSmooth);
 
         var inputZ = Input.GetAxis("Vertical");
         var inputX = Input.GetAxis("Horizontal");
         
         var angle = Mathf.Atan2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0, angle, 0);

The problem I have is that I need to determine the speed of the object to affect animation speed.

I cant use the joystick axis as it goes from -1 to +1 and I cannot use rigidbody.velocity.magnitude as I am not using forces.

Is there any way I can work out the speed of the object from a scale of 0 to 1?

Comment

People who like this

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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by MakinStuffLookGood · Jan 22, 2015 at 09:13 PM

You can't use the joystick because it goes from -1 to 1? Could you not just use Mathf.Abs(Input.GetAxis("Horizontal")) to get a value in the correct range then?

If that's no good, just store your position every frame in some variable Vector3 prevPosition and each frame you can do something like:

 float instantaneousSpeed = Vector3.Distance(transform.position, prevPosition);
Comment
NerdRageStudios

People who like this

1 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 NerdRageStudios · Jan 22, 2015 at 09:26 PM 0
Share

Thanks Broxxar, I created the following and it works great :)

         float x = Mathf.Abs(inputX);
         float z = Mathf.Abs(inputZ);
         float s = Mathf.Max(z, x);
avatar image

Answer by carrollh · Jan 22, 2015 at 08:07 PM

Since you have a rigidbody, why don't you just use Rigidbody.MovePosition and Rigidbody.MoveRotation? Then I think you could use the rigidbody.velocity.magnitude that you mentioned. You might need to normalize it first to get it into unit vector form. So your code might look like this:

 rigidbody.MovePosition(rigidbody.position + moveSpeed * Time.deltaTime);

 float velMag = rigidbody.velocity.magnitude;
 animator.SetFloat("Speed", velMag / moveSpeed); 

Comment

People who like this

0 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 NerdRageStudios · Jan 22, 2015 at 08:49 PM 0
Share

Hey thanks for helping! I tried that but when I put velMag to Debug.Log its always showing 0

Any ideas?

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

21 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

Related Questions

Move camera when mouse is near the edges of the screen 1 Answer

How to get a proper ramming effect? 1 Answer

MOVE GameObject to point A to B and to B to A 2 Answers

collider triggers transform position 1 Answer

How do I change one value of a vector? 2 Answers


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